方法一: 1 public static bool ExecuteSqlByTrans(List<SqlAndPrams> list) 2 { 3 bool success = true; 4 Open(); 5 SqlCommand cmd = new SqlCommand(); 6 SqlTransaction trans = Connection.BeginTransaction(); 7 cmd.Connection = Connection; 8 cmd.Transaction = trans; 9 try 10 { 11 foreach (SqlAndPrams item in list) 12 { 13 if (item.cmdParms == null) 14 { 15 cmd.CommandText = item.sql; 16 cmd.ExecuteNonQuery(); 17 } 18 else 19 { 20 cmd.CommandText = item.sql; 21 cmd.CommandType = CommandType.Text;//cmdType; 22 cmd.Parameters.Clear(); 23 foreach (SqlParameter parameter in item.cmdParms) 24 { 25 if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) 26 { 27 parameter.Value = DBNull.Value; 28 } 29 cmd.Parameters.Add(parameter); 30 } 31 cmd.ExecuteNonQuery(); 32 } 33 34 } 35 trans.Commit(); 36 } 37 catch (Exception e) 38 { 39 success = false; 40 trans.Rollback(); 41 } 42 finally 43 { 44 Close(); 45 } 46 return success; 47 } 方法一对应的实体类:
1 public class SqlAndPrams 2 { 3 public string sql { get; set; } 4 5 public SqlParameter[] cmdParms { get; set; } 6 }
DAL层代码:
1 /// <summary> 2 /// 增加一条数据 3 /// </summary> 4 public SqlAndPrams AddAccount(Entity.Account_T model) 5 { 6 SqlAndPrams result = new SqlAndPrams(); 7 StringBuilder strSql = new StringBuilder(); 8 strSql.Append("insert into Account_T("); 9 strSql.Append("AccountNo,Type,Count,LoginName,Remark,CreateTime)"); 10 strSql.Append(" values ("); 11 strSql.Append("@AccountNo,@Type,@Count,@LoginName,@Remark,@CreateTime)"); 12 strSql.Append(";select @@IDENTITY"); 13 SqlParameter[] parameters = { 14 new SqlParameter("@AccountNo", SqlDbType.NVarChar,20), 15 new SqlParameter("@Type", SqlDbType.NVarChar,10), 16 new SqlParameter("@Count", SqlDbType.Int,4), 17 new SqlParameter("@LoginName", SqlDbType.NVarChar,30), 18 new SqlParameter("@Remark", SqlDbType.NVarChar,50), 19 new SqlParameter("@CreateTime", SqlDbType.DateTime)}; 20 parameters[0].Value = model.AccountNo; 21 parameters[1].Value = model.Type; 22 parameters[2].Value = model.Count; 23 parameters[3].Value = model.LoginName; 24 parameters[4].Value = model.Remark; 25 parameters[5].Value = model.CreateTime; 26 27 result.sql = strSql.ToString(); 28 result.cmdParms = parameters; 29 return result; 30 }
调用层:
1 List<SqlAndPrams> updateList = new List<SqlAndPrams>(); 2 Entity.Account_T model_account = new Entity.Account_T(); 3 model_account.CreateTime = DateTime.Now; 4 model_account.LoginName = userName; 5 model_account.Type = "消费"; 6 model_account.Count = -num; updateList.Add(dal_tran.AddAccount(model_account)); //添加进事务 7 DbHelperSQL.ExecuteSqlByTrans(updateList) //调用执行
方法二:暂时没有用,没有调用例子
1 public static bool ExecuteSQL2(string[] SqlStrings) 2 { 3 bool success = true; 4 Open(); 5 SqlCommand cmd = new SqlCommand(); 6 SqlTransaction trans = Connection.BeginTransaction(); 7 cmd.Connection = Connection; 8 cmd.Transaction = trans; 9 try 10 { 11 foreach (string str in SqlStrings) 12 { 13 cmd.CommandText = str; 14 cmd.ExecuteNonQuery(); 15 } 16 trans.Commit(); 17 } 18 catch 19 { 20 success = false; 21 trans.Rollback(); 22 } 23 finally 24 { 25 Close(); 26 } 27 return success; 28 }
原文地址:https://www.cnblogs.com/xujunbao/p/9726913.html
时间: 2024-10-12 16:26:37