扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
/**//// <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()方法调用 "); } } |
/**//// <summary> /// 测试 /// </summary> /// <param name="sender"> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { try { Console.WriteLine("下面是使用事务的输出......"); this.TestACT(); Console.WriteLine("*********************下面是 未 使用事务的输出"); this.TestNoACT(); } catch (System.Exception ex) { Console.WriteLine("出现了异常????????????"); MessageBox.Show(ex.ToString()); } } /**//// <summary> /// 使用事务 /// </summary> public void TestACT() { DataAccessTest dac = new DataAccessTest(); using (Scope scope = new Scope(dac)) { string strSql1 = "INSERT INTO [tilva20].[dbo].[T_Test]([TestName])VALUES('a')"; string strSql2 = "INSERT INTO [tilva20].[dbo].[T_Test]([TestName])VALUES('b')"; dac.ExecuteSql(strSql1); dac.ExecuteSql(strSql2); scope.Complete(); } } /**//// <summary> /// 不使用事务 /// </summary> public void TestNoACT() { DataAccessTest dac = new DataAccessTest(); string strSql1 = "INSERT INTO [tilva20].[dbo].[T_Test]([TestName])VALUES('a')"; string strSql2 = "INSERT INTO [tilva20].[dbo].[T_Test]([TestName])VALUES('b')"; dac.ExecuteSql(strSql1); dac.ExecuteSql(strSql2); } |
/**//// <summary> /// 使用事务 /// </summary> public void TestACT() { DataAccessTest dac = new DataAccessTest(); using (Scope scope = new Scope(dac)) { string strSql1 = "INSERT INTO [tilva20].[dbo].[T_Test]([TestName])VALUES('a')"; string strSql2 = "INSERT INTO [tilva20].[dbo].[T_Test]([TestName])VALUES('b222222222 22222222222222222222222222222222222222222222222222222222222222222222222222222')"; dac.ExecuteSql(strSql1); dac.ExecuteSql(strSql2); scope.Complete(); } } |
public int UpdateTest(OutSourcing outSourcing, BusinessAccept businessAccept) { IDataAccess dac = DataAccess.Create(); DAHelper myHelper = new DAHelper(dac); using (Scope ts=new Scope()) { myHelper.Update(outSourcing); myHelper.Update(businessAccept); ts.Complete(); } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者