在項目中我是這樣處理數據的:
/**//// <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);
}
}
/// 外包業務訪問類
/// </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);
}
}
上面 OutSourcing 為與表相對應的實體類 ;DAHelper 為作者自己實現的一個包裝類,可以對任意實體進行增加,修改,刪除 查詢等功能。
再貼一段代碼:
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();
}
}
{
try
{
this.OpenConnection();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql;
return cmd.ExecuteNonQuery();
}
catch (System.Exception e)
{
throw this.CatchException(e);
}
finally
{
this.CloseConnection();
}
}
我最終通過調用 ExecSql 方法來與數據庫交互,而該方法會自己打開數據庫連接,執行語句,然后關閉連接。
在操作同一個數據庫的時候,如果要高效的使用 TransactionScope,必須保證 SqlConnection 不改變,即用同一個 SqlConnection 來完成所需要的增加刪除,或修改。我想寫這樣的代碼進行事務控制:
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();
}
}
{
IDataAccess dac = DataAccess.Create();
DAHelper myHelper = new DAHelper(dac);
using (TransactionScope ts = new TransactionScope())
{
myHelper.Update(outSourcing);
myHelper.Update(businessAccept);
ts.Complete();
}
}
這樣就需要,執行第一個操作時候打開數據庫連接,執行,不關閉連接,然后執行第二個操作,執行完關閉。顯然,我想讓 TransactionScope 在 實行 Dispose() 方法的時候關閉數據庫連接。using 代碼塊的本質上等同于 try{}finally{} 語句代碼塊。為什么不封裝一下 TransactionScope 讓它滿足自己的要求呢?
/**//// <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
}
/// 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()方法調用 ");
}
}
/// 模擬數據庫訪問類
/// </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()方法調用 ");
}
}