zoukankan      html  css  js  c++  java
  • 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.         }  
  • 相关阅读:
    一切都是对象
    对象入门
    同步计算输入的各个数的总和与平均值
    与时间有关的类Date,DateFormat,Calendar
    获取文件信息
    串行化
    分解
    高速缓存
    压缩
    MyCAT实现MySQL的读写分离
  • 原文地址:https://www.cnblogs.com/majiabin/p/4885587.html
Copyright © 2011-2022 走看看