• sqlserver事务调用代码


    1. static void Main(string[] args)  
    2.         {  
    3.   
    4.             SqlConnection sqlConn = new SqlConnection(  
    5.                 ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);  
    6.             SqlTransaction sqlTrans = null;  
    7.             try  
    8.             {  
    9.                 sqlConn.Open();  
    10.                 sqlTrans = sqlConn.BeginTransaction();//事务开始  
    11.                 SqlCommand sqlComm = new SqlCommand("", sqlConn, sqlTrans);  
    12.                 sqlComm.CommandTimeout = 120;  
    13.                 sqlComm.CommandType = System.Data.CommandType.Text;  
    14.   
    15.                 string insertSql = "insert into dbo.TransTestTable values (66,'66');";  
    16.                 string updateSql = "update dbo.TransTestTable set [Name] = '77' where [Id] = 66;";  
    17.   
    18.                 sqlComm.CommandText = insertSql;  
    19.                 sqlComm.ExecuteNonQuery();//执行insert  
    20.   
    21.                 sqlComm.CommandText = updateSql;  
    22.                 sqlComm.ExecuteNonQuery();//执行update  
    23.                 //throw new Exception("test exception.the transaction must rollback");  
    24.   
    25.                 sqlTrans.Commit();//事务提交  
    26.             }  
    27.             catch (Exception ex)  
    28.             {  
    29.                 sqlTrans.Rollback();//事务回滚  
    30.                 Console.WriteLine(ex.Message);  
    31.             }  
    32.             finally  
    33.             {  
    34.                 if (sqlConn.State != System.Data.ConnectionState.Closed)  
    35.                     sqlConn.Close();  
    36.             }  
    37.   
    38.             Console.ReadLine();  
    39.         }  
  • 相关阅读:
    Magento 安装时文件权限 设置
    进度十(10.28)
    进度九(10.27)
    进度八(10.26)
    进度六(10.24)
    进度五(10.23)
    进度四(10.22)
    进度三(10.21)
    进度二(10.20)
    进度一(10.19)
  • 原文地址:https://www.cnblogs.com/majiabin/p/4885587.html
Copyright © 2020-2023  润新知