日期:2014-05-17 浏览次数:20665 次
public static int ExecuteAsTransaction(string CommandText, List<Parameter[]> ParamsList)
{
//...省略部分代码 已经获取连接Sconn
SqlCommand cmd = new SqlCommand();
cmd.Connection = Sconn;
SqlTransaction transaction = Sconn.BeginTransaction(); //开始事务 cmd.Transaction = transaction;
bool flag = false;
try
{
foreach (Parameter[] Params in ParamsList)
{
AddParameter(ref cmd, Params); //增加参数
cmd.CommandText = CommandText;
cmd.ExecuteNonQuery();
}
transaction.Commit();
flag = true;
}
catch (Exception ex1)
{
transaction.Rollback();
flag = false;
throw new Exception(ex1.Message);
}
finally
{
//
}
return (flag ? 0 : -2);
}
private static void AddParameter(ref SqlCommand Cmd, params Parameter[] Params)
{
//如果SqlCommand对象和参数列表不为空,则进行添加参数操作
&