-2

いくつかの挿入ステートメントを使用して、mysql データベースを使用して winform でトランザクションを作成しました。これらの挿入ステートメントをストアド プロシージャとして使用する方法。

4

1 に答える 1

0

以下は役に立つかもしれません...

MySQL

drop table if exists users;
create table users
(
user_id int unsigned not null auto_increment primary key,
username varchar(32) unique not null
)
engine=innodb;

insert into users (username) values ('foo'),('test');

select * from users order by user_id;


drop procedure if exists update_user;

delimiter #
create procedure update_user
(
in p_user_id int unsigned,
in p_username varchar(32)
)
proc_main:begin
    update users set username = p_username where user_id = p_user_id; 
end proc_main #

delimiter ;

call update_user(2,'bar');

select * from users order by user_id;

C#non sproc(コンソールの例)

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace TransDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            const string DB_CONN_STR = "Persist Security Info=False;Username=foo_dbo;Password=pass;database=foo_db;server=localhost;Connect Timeout=30";
            const int DB_TIMEOUT = 30;

            MySqlConnection cn = new MySqlConnection(DB_CONN_STR);
            MySqlCommand cmd = null;
            MySqlTransaction trx = null;

            Console.WriteLine("Updating...");

            try
            {
                cn.Open();
                string sqlCmd = "update users set username = @username where user_id = @user_id";

                cmd = new MySqlCommand(sqlCmd, cn);

                cmd.CommandType = CommandType.Text;
                cmd.CommandTimeout = DB_TIMEOUT;

                cmd.Parameters.AddWithValue("@username", "beta");
                cmd.Parameters.AddWithValue("@user_id", 2);

                cmd.Prepare();

                trx = cn.BeginTransaction();
                cmd.Transaction = trx;

                cmd.ExecuteNonQuery();

                trx.Commit();

                Console.WriteLine("Update complete");
            }
            catch (Exception ex)
            {
                if(trx != null) trx.Rollback();
                Console.WriteLine("oops - {0}", ex.Message);
            }
            finally
            {
                cn.Dispose();
            }
            Console.WriteLine("Press any key to quit");
            Console.ReadKey();
        }
    }
}

C#sproc(コンソールの例)

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace TransDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            const string DB_CONN_STR = "Persist Security Info=False;Username=foo_dbo;Password=pass;database=foo_db;server=localhost;Connect Timeout=30";
            const int DB_TIMEOUT = 30;

            MySqlConnection cn = new MySqlConnection(DB_CONN_STR);
            MySqlCommand cmd = null;
            MySqlTransaction trx = null;

            Console.WriteLine("Updating...");

            try
            {
                cn.Open();
                string sqlCmd = "update_user";

                cmd = new MySqlCommand(sqlCmd, cn);

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandTimeout = DB_TIMEOUT;

                cmd.Parameters.AddWithValue("@p_username", "alpha");
                cmd.Parameters.AddWithValue("@p_user_id", 2);

                // cmd.Prepare(); // no need to prepare it's a stored proc

                trx = cn.BeginTransaction();
                cmd.Transaction = trx;

                cmd.ExecuteNonQuery();

                trx.Commit();

                Console.WriteLine("Update complete");
            }
            catch (Exception ex)
            {
                if(trx != null) trx.Rollback();
                Console.WriteLine("oops - {0}", ex.Message);
            }
            finally
            {
                cn.Dispose();
            }
            Console.WriteLine("Press any key to quit");
            Console.ReadKey();
        }
    }
}

お役に立てれば :)


于 2012-06-28T13:09:09.403 に答える