标签:执行 action pen connect man stat new 开启 update
#region 调用事务
public static bool UpdateByTran(List
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
try
{
conn.Open();
//开启事务
cmd.Transaction = conn.BeginTransaction();
//遍历所有sql语句,依次执行
foreach (string item in sqlList)
{
cmd.CommandText = item;
cmd.ExecuteNonQuery();
}
//如果全部执行完毕,提交事务
cmd.Transaction.Commit();
return true;
}
catch (Exception ex)
{
//判断是不是由于事务运行导致,如果是,就回滚事务
if (cmd.Transaction != null)
{
cmd.Transaction.Rollback();//回滚事务
}
throw ex;
}
finally
{
if (cmd.Transaction != null)
{
cmd.Transaction = null;//释放事务资源
}
conn.Close();//关闭连接
}
}
#endregion
标签:执行 action pen connect man stat new 开启 update
原文地址:https://www.cnblogs.com/zhujie-com/p/12189905.html