/// <summary> /// 事务处理 /// </summary> /// <param name="sqlList">sql语句数组</param> /// <returns></returns> /// <exception cref="Exception"></exception> public static bool OpenTransation(List<string> sqlList) { SqlConnection conn = new SqlConnection(connStr); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; try { conn.Open(); //打开 cmd.Transaction = conn.BeginTransaction(); //开启事务 foreach (var sql in sqlList) { cmd.CommandText = sql; cmd.ExecuteNonQuery(); } cmd.Transaction.Commit(); //执行事务 return true; } catch (Exception ex) { //出现错误,回滚之前执行的操作 cmd.Transaction.Rollback(); //回滚 conn.Close(); //关闭 throw new Exception(ex.Message); } finally { cmd.Transaction = null; //初始化 conn.Close(); } }
标签:事务,Transaction,封装,cmd,sql,new,ADO,NET,conn From: https://www.cnblogs.com/tlfe/p/18253671