/// <summary>
/// 执行事务
/// </summary>
/// <param name="sqlList">SQL语句列表</param>
/// <returns></returns>
public bool ExecuteTransaction(List<string> SqlList)
{
bool rst = true;
SqlConnection sqlConnection = new SqlConnection(connStr);
sqlConnection.Open();
//...初始化连接
// 开启事务
SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();
// 将事务应用于Command
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.Transaction = sqlTransaction;
try
{
// 利用sqlcommand进行数据操作
//...
foreach (string strsql in SqlList)
{
sqlCommand.CommandText = strsql;
sqlCommand.ExecuteNonQuery();
}
// 成功提交
sqlTransaction.Commit();
}
catch (Exception ex)
{
// 出错回滚
rst = false;
sqlTransaction.Rollback();
}
finally
{
sqlConnection.Close();
sqlTransaction.Dispose();
sqlConnection.Dispose();
}
return rst;
}
/// 执行事务
/// </summary>
/// <param name="sqlList">SQL语句列表</param>
/// <returns></returns>
public bool ExecuteTransaction(List<string> SqlList)
{
bool rst = true;
SqlConnection sqlConnection = new SqlConnection(connStr);
sqlConnection.Open();
//...初始化连接
// 开启事务
SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();
// 将事务应用于Command
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.Transaction = sqlTransaction;
try
{
// 利用sqlcommand进行数据操作
//...
foreach (string strsql in SqlList)
{
sqlCommand.CommandText = strsql;
sqlCommand.ExecuteNonQuery();
}
// 成功提交
sqlTransaction.Commit();
}
catch (Exception ex)
{
// 出错回滚
rst = false;
sqlTransaction.Rollback();
}
finally
{
sqlConnection.Close();
sqlTransaction.Dispose();
sqlConnection.Dispose();
}
return rst;
}