zoukankan      html  css  js  c++  java
  • 事务 c#

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

                  ->事务具有四个特性:原子性、隔离性、一致性、持久性。这四个特性通常称为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;

                    }

  • 相关阅读:
    ASP.NET MVC 3 Internationalization
    Windows 8学习笔记(十)Notification
    Windows 8学习笔记(十二)集合控件
    Windows 8学习笔记(七)Input输入设备
    【配置与安装】解决类似umount target is busy挂载盘卸载不掉问题
    【coredump】coredump 使用
    【C++调试】"./xxx: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by ./xxx)" 执行报错
    【配置与安装】CentOS7 多磁盘合成卷组并创建LVM,挂载到同一目录
    Unix 知识
    3D创作概念入门
  • 原文地址:https://www.cnblogs.com/tony-brook/p/7803300.html
Copyright © 2011-2022 走看看