.NET Framework 2.0 版中新增的 TransactionScope 单独使用确实很方便。但是在实际项目中都有自己的访问层,如何才能和自己的数据访问层结合起来使用呢?
在项目中我是这样处理数据的:
/**//// <summary>
/// 外包业务访问类
/// </summary>
public class OutSourcingDAO
{
/**//// <summary>
/// 增加
/// </summary>
/// <param name="bt"></param>
/// <returns></returns>
public int InsertGetIdentity(OutSourcing bt)
{
return new DAHelper(DataAccess.Create()).InsertGetIdentity(bt);
}
/**//// <summary>
/// 更新
/// </summary>
/// <param name="bt"></param>
/// <returns></returns>
public int Update(OutSourcing bt)
{
return new DAHelper(DataAccess.Create()).Update(bt);
}
/**//// <summary>
/// 删除
/// </summary>
/// <param name="bt"></param>
/// <returns></returns>
public int Delete(OutSourcing bt)
{
return new DAHelper(DataAccess.Create()).Delete(bt);
}
}
public int ExecSql(string strSql)
{
try
{
this.OpenConnection();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql;
return cmd.ExecuteNonQuery();
}
catch (System.Exception e)
{
throw this.CatchException(e);
}
finally
{
this.CloseConnection();
}
}
public int UpdateTest(OutSourcing outSourcing, BusinessAccept businessAccept)
{
IDataAccess dac = DataAccess.Create();
DAHelper myHelper = new DAHelper(dac);
using (TransactionScope ts = new TransactionScope())
{
myHelper.Update(outSourcing);
myHelper.Update(businessAccept);
ts.Complete();
}
}
/**//// <summary>
/// TransactionScope 包装类
/// </summary>
public sealed class Scope : IDisposable
{
private TransactionScope m_TransactionScope = null;
/**//// <summary>
/// 测试访问类
/// </summary>
private DataAccessTest m_DataAccessTest = null;
/**//// <summary>
/// 实例化一个新的 TransactionScope
/// </summary>
/// <param name="dac"></param>
public Scope(DataAccessTest dac)
{
this.m_DataAccessTest = dac;
//告诉访问类 你已经使用了事务
dac.SetScope(this);
this.m_TransactionScope = new TransactionScope();
}
/**//// <summary>
/// 发出事务结束命令
/// </summary>
public void Complete()
{
this.m_TransactionScope.Complete();
}
IDisposable 成员#region IDisposable 成员
/**//// <summary>
/// 当执行该方法的时候完成两件任务
/// 1 关闭数据库栀湩???? ??魀?(连接
/// 2 调用 TransactionScope 的 Dispose()方法
/// </summary>
void IDisposable.Dispose()
{
try
{
m_DataAccessTest.Close();
}
finally
{
m_TransactionScope.Dispose();
}
}
#endregion
}
/**//// <summary>
/// 模拟数据库访问类
/// </summary>
public class DataAccessTest
{
SqlConnection con = null;
SqlCommand cmd = new SqlCommand();
Scope scope = null;
string strCon = "这里是数据库连接字符串。。。。。";
public void SetScope(Scope scope)
{
this.scope = scope;
}
private void OpenConnection()
{
if (con == null || scope == null)
{
con = new SqlConnection(strCon);
cmd.Connection = con;
con.Open();
Console.WriteLine(" 打开数据库连接;");
}
}
private void CloseConnection()
{
this.cmd.Parameters.Clear();
if (scope == null)
{
//
con.Close();
con.Dispose();
Console.WriteLine(" 未使用事务 关闭数据库连接;");
}
}
public int ExecuteSql(string strSql)
{
try
{
this.OpenConnection();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql;
Console.WriteLine("执行 Sql 语句。。。");
return cmd.ExecuteNonQuery();
}
catch (System.Exception e)
{
throw e;
}
finally
{
this.CloseConnection();
}
}
public void Close()
{
con.Close();
con.Dispose();
Console.WriteLine(" 关闭数据库连接->该方法由 Scope中的Dispose()方法调用 ");
}
}