C#下应⽤事务循环操作Sql语句 1///<summary>
2///执⾏多条Sql语句,实现事务回滚
3///</summary>
4///<param name="SQLStringList">sql语句集合</param>
5///<param name="ErrorMsg">执⾏多SQL语句事务,返回错误信息</param>
6///<returns></returns>
7public static bool SqlTransSqlStr(List<string> SQLStringList, ref string ErrorMsg)
8 {
9bool i = false;
10string connectionString = ConfigurationManager.ConnectionStrings["demo"].ConnectionString;
11using (SqlConnection connection = new SqlConnection(connectionString))
12 {
13 connection.Open();
14 SqlCommand cmd = new SqlCommand();
15 cmd.Connection = connection;
16 SqlTransaction tx = connection.BeginTransaction();
17 cmd.Transaction = tx;
18try
19 {
sqltransaction什么意思20for (int n = 0; n < SQLStringList.Count; n++)
21 {
22string strsql = SQLStringList[n].ToString();
23if (strsql.Trim().Length > 1)
24 {
25 cmd.CommandText = strsql;
26 cmd.ExecuteNonQuery();
27 cmd.Parameters.Clear();
28 }
29 }
30 tx.Commit();
31 i = true;
32 }
33catch (System.Data.SqlClient.SqlException E)
34 {
35 tx.Rollback();
36 ErrorMsg = E.Message;
37 i = false;
38 }
39 }
40return i;
41 }
直接代码,改⼀下配置⽂件⾥⾯的字符串的名字就OK 了。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论