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.         }  
  • 相关阅读:
    .vue文件在phpstorm中红线解决办法
    javascript预编译详解
    javascript数据类型判断及数据隐式和显示转换
    用git版本库工具上传项目到github(新手简单易上手)
    javascript的数据运算和条件判断
    javascript基础类型和引用类型
    uniapp开发钉钉小程序遇到的坑!!!
    pdf解析器 用于小程序 h5
    百度的Ueditor(php)上传到aws s3存储方案
    css3 pointer-event属性
  • 原文地址:https://www.cnblogs.com/majiabin/p/4885587.html
Copyright © 2011-2022 走看看