事务 c#

2021-04-30 15:27

阅读:574

标签:表示   cut   cat   cmd   ack   on()   tran   back   add   

事务->:事务是恢复和并发控制的基本单位

              ->事务具有四个特性:原子性、隔离性、一致性、持久性。这四个特性通常称为ACID

       Begin transaction/tran   --开始事务

       Commit transaction/tran  --提交事务

       Rollback transaction/tran  --回滚事务

 

Select * from userinfo with(nolock)       --表示在查询的时候不加共享锁

 

一个简单的小案例

 

using (SqlConnection conn = new SqlConnection(connStr))

            {

                conn.Open();

                //开始事务

                SqlTransaction ts = conn.BeginTransaction();

                try

                {

                    string sql = "insert into BuyRecord(UID,PID,BRNum,BRTime) values(@uid,@pid,@num,@time)";

                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.Transaction = ts;

                    cmd.Parameters.AddWithValue("@uid", brm.UID);

                    cmd.Parameters.AddWithValue("@pid", brm.PID);

                    cmd.Parameters.AddWithValue("@num", brm.BRNum);

                    cmd.Parameters.AddWithValue("@time", DateTime.Now);

                    cmd.ExecuteNonQuery();

                    string uptsql = " update ProductInfo set ProductNum=ProductNum-@buyNum where ProductID=@id";

                    cmd.CommandText = uptsql;

                    cmd.Parameters.AddWithValue("@buyNum", brm.BRNum);

                    cmd.Parameters.AddWithValue("@id", brm.PID);

                    cmd.ExecuteNonQuery();

 

                    ts.Commit();

                    return 1;

                }

                catch (Exception)

                {

                    ts.Rollback();

                    return -1;

                    throw;

                }

事务 c#

标签:表示   cut   cat   cmd   ack   on()   tran   back   add   

原文地址:http://www.cnblogs.com/tony-brook/p/7803300.html


评论


亲,登录后才可以留言!