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         }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

 作者:꧁执笔小白꧂