-2

我在带有一些插入语句的 mysql 数据库中创建了一个事务。如何将这些插入语句用作存储过程。

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# 非存储过程(控制台示例)

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 回答