zoukankan      html  css  js  c++  java
  • 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个夜晚,我希望做到两天更一篇博客。加油,小白!
  • 相关阅读:
    Socket通信中的多进程编程(TCP/IP通信过程)
    AsyncSocket.h解读
    AsyncSocket编程
    命令行下文件名空格的处理
    socket编程 Asyncsocket (转)
    iPhone网络编程初体验简单的聊天程序 (转)
    2.0版本cocos2diphone 开发之CCProgressTimer制作游戏中的血条
    js 旋转图片
    css图片旋转
    数据结构之单向链表
  • 原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/15396866.html
Copyright © 2011-2022 走看看