MSSQL数据库事务

复制代码
 1         /// <summary>
 2         /// 执行多条SQL语句,实现数据库事务。
 3         /// </summary>
 4         /// <param name="SQLStringList">多条SQL语句</param>        
 5         public static int ExecuteSqlTran(List<String> SQLStringList)
 6         {
 7             using (SqlConnection conn = new SqlConnection(connectionString))
 8             {
 9                 conn.Open();
10                 SqlCommand cmd = new SqlCommand();
11                 cmd.Connection = conn;
12                 SqlTransaction tx = conn.BeginTransaction();
13                 cmd.Transaction = tx;
14                 try
15                 {
16                     int count = 0;
17                     for (int n = 0; n < SQLStringList.Count; n++)
18                     {
19                         string strsql = SQLStringList[n];
20                         if (strsql.Trim().Length > 1)
21                         {
22                             cmd.CommandText = strsql;
23                             count += cmd.ExecuteNonQuery();
24                         }
25                     }
26                     tx.Commit();
27                     return count;
28                 }
29                 catch
30                 {
31                     tx.Rollback();
32                     return 0;
33                 }
34             }
35         }
复制代码

 

posted @   ꧁执笔小白꧂  阅读(126)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
历史上的今天:
2021-01-20 Android的Looper.loop()消息循环机制
2021-01-20 申请读写sd卡权限shell
2021-01-20 Linux系统知识(四)-Shell脚本学习笔记
2021-01-20 Android Studio常用快捷方式
2021-01-20 Android ContentProvider操作其他应用程序共享的数据
点击右上角即可分享
微信分享提示