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.         }  
  • 相关阅读:
    Halcon 笔记3 形态学
    Halcon 笔记2 Blob分析
    Halcon 笔记1
    线程
    Fn+F1-F12,避免使用FN+
    改变与接受
    PictureBox使用异常
    (一)Knockout
    (二)HTML5
    (一)chrome扩展
  • 原文地址:https://www.cnblogs.com/majiabin/p/4885587.html
Copyright © 2011-2022 走看看