C# 方法 执行多条SQL语句,实现数据库事务

 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 (Exception err)
30                 {
31                     tx.Rollback();
32                   
33                     return 0;
34                 }
35             }
36                   
37         }
原文地址:https://www.cnblogs.com/surfacebook007/p/9263343.html