批量更新的时候 传递 SqlParameter的问题:
代码如下:
string[] sql = new string[4]; StringBuilder str = new StringBuilder(); str.Append(@" update A_ObjectInfo set ObjectRegNum=@ObjectReg "); str.AppendFormat(@" where ObjectID='{0}'", ObjectID); SqlParameter[] spr = new SqlParameter[]{ new SqlParameter("@ObjectReg",SqlDbType.VarChar,50) }; spr[0].Value = ObjectInfoModel.A_ObjectRegNum; sql[0] = str.ToString(); str.Clear(); str.Append(@" update A_RegisterModel set Writer=@Writer,WriteDate=@WriteDate,ContractNo=@ContractNo,ServiceFee=@ServiceFee "); str.AppendFormat(@" where ObjectID='{0}'", ObjectID); SqlParameter[] spr1 = new SqlParameter[]{ new SqlParameter("@Writer",SqlDbType.VarChar,50), }; spr1[0].Value = RegisterModel.A_Writer; sql[1] = str.ToString(); str.Clear(); return Commit.Commit.executeTransaction(sql,????); ???代码是 SqlParameter(有2个)的
executeTransaction的代码:
bool flag = false; SqlCommand cmd = new SqlCommand(); cmd.Connection = _con;//命令对象 SqlTransaction myTransaction;//声明一个SQL事物类型 myTransaction = con.BeginTransaction();//基于一个连接初始化事物 try { foreach (string sql in strSql)//遍历 { cmd.Transaction = myTransaction;//指定SQL命令语句 的事物 cmd.CommandText = sql;//给定命令语句 if (values != null) { foreach (SqlParameter parameter in values) { if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } cmd.Parameters.Add(parameter); } } cmd.ExecuteNonQuery();//执行SQL语句 } myTransaction.Commit(); flag = true; } catch (Exception ex)
我如何把SqlParameter传递过去 并且执行的时候添加对应的 cmd.Parameters.Add(parameter);???
大侠们明白了我的意思吗?
白猪掌柜的
翻过高山走不出你
翻阅古今