TranscationScope用法示例
//标记是否已执行接口库中的操作
bool isYiZhiXingInterface = false;
using (TransactionScope transScope = new TransactionScope())
{
DataBase dbLocal = new DataBase(_connectionString);
DataBase dbInterface = new DataBase(_connectionStringInterface);
try
{
dbLocal.BeginTrans();
dbInterface.BeginTrans();
dbLocal.RunSqlNonQuery_Trans(strSql);
dbInterface.RunSqlNonQuery_Trans(strSqlInterface);
isYiZhiXingInterface = true;
}
catch(System.Data.SqlClient.SqlException ex)
{
dbLocal.RollBack();
if (isYiZhiXingInterface)
dbInterface.RollBack();
dbLocal.Dispose();
dbInterface.Dispose();
throw ex;
}
dbLocal.Commit();
dbInterface.Commit();
transScope.Complete();
dbLocal.Dispose();
dbInterface.Dispose();
transScope.Dispose();
}
//标记是否已执行接口库中的操作
bool isYiZhiXingInterface = false;
using (TransactionScope transScope = new TransactionScope())
{
DataBase dbLocal = new DataBase(_connectionString);
DataBase dbInterface = new DataBase(_connectionStringInterface);
try
{
dbLocal.BeginTrans();
dbInterface.BeginTrans();
dbLocal.RunSqlNonQuery_Trans(strSql);
dbInterface.RunSqlNonQuery_Trans(strSqlInterface);
isYiZhiXingInterface = true;
}
catch(System.Data.SqlClient.SqlException ex)
{
dbLocal.RollBack();
if (isYiZhiXingInterface)
dbInterface.RollBack();
dbLocal.Dispose();
dbInterface.Dispose();
throw ex;
}
dbLocal.Commit();
dbInterface.Commit();
transScope.Complete();
dbLocal.Dispose();
dbInterface.Dispose();
transScope.Dispose();
}