在程序中使用事务处理

SqlConnection sqlConnection = new SqlConnection();
…初始化连接

// 开启事务
SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();

// 将事务应用于Command
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.Transaction = sqlTransaction;
try
{
// 利用sqlcommand进行数据操作

// 成功提交
sqlTransaction.Commit();
}
catch(Exception ex)
{
// 出错回滚
sqlTransaction.Rollback();
}

猜你喜欢

转载自blog.csdn.net/lnazj/article/details/80145447