• C# Sql事务写法示例


    using System.Data.SqlClient;



    1
    string idStr="123"; 2 using (SqlConnection conn = new SqlConnection(Appconfig.GetMSSQLStr())) 3 { 4 5 conn.Open(); 6 using (SqlTransaction trans = conn.BeginTransaction()) 7 { 8 try 9 { 10 SqlCommand cmd = new SqlCommand(); 11 cmd.Connection = conn; 12 cmd.Transaction = trans; 13 14 StringBuilder strHead = new StringBuilder(); 15 strHead.AppendLine("*********日志记录**********:"); 16 strHead.AppendLine("......"); 17 strHead.AppendLine("*********日志记录**********:"); 18 Appconfig.WriteLogFile(strHead.ToString(), "日志文件"); 19 20 string chksql = ""; 21 #region sql语句1 22 chksql = @"select * from table1 where id='{0}'"; 23 24 chksql = string.Format(chksql, idStr); 25 26 cmd.CommandText = chksql; 27 cmd.ExecuteNonQuery(); 28 #endregion 29 30 #region sql语句2 31 chksql = @"select * from table2 where id='{0}'"; 32 33 chksql = string.Format(chksql, idStr); 34 35 cmd.CommandText = chksql; 36 cmd.ExecuteNonQuery(); 37 #endregion 38 39 40 trans.Commit(); 41 42 StringBuilder strHead = new StringBuilder(); 43 resultlog.AppendLine("执行完毕"); 44 Appconfig.WriteLogFile(resultlog.ToString(), "日志文件"); 45 46 } 47 catch (Exception ex) 48 { 49 trans.Rollback(); 50 StringBuilder str = new StringBuilder(); 51 Appconfig.WriteLogFile(str.ToString(), "日志文件"); 52 } 53 } 54 conn.Close(); 55 }
    365个夜晚,我希望做到两天更一篇博客。加油,小白!
  • 相关阅读:
    HTTP协议
    从Iterator到async/await
    那些年曾谈起的跨域
    设计模式之观察者模式与发布订阅模式
    移动Web深度剖析
    浅析JavaScript异步
    mySql入门-(二)
    C# WEB项目MVC框架原理及约定
    Dynamics CRM 邮箱设置 “允许使用凭据进行电子邮件处理” 被禁用的解决
    Win10系统恢复IE 11浏览器
  • 原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/15396866.html
Copyright © 2020-2023  润新知