最近在开发过程中用到比较多的批处理让我感觉ADO.NET2.0的事务真的是方便,记得以前的都是: SqlTransaction trans;
trans = con.BeginTransaction(); cmd.Transaction = trans; try { cmd.ExecuteNonQuery(); trans.Commit(); } catch(Exception e) { trans.Rollback(); } finally { con.Close(); } 一大堆东西麻烦,看看MSDN中ADO.NET2.0里面的事务: 标准的事务测试方法 1/**//// <summary> 2 /// 事务测试方法-- Add by Teracy on 2007-09-09 3 /// </summary> 4 void TestTransaction() 5 { 6 TransactionOptions options = new TransactionOptions(); 7 options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted; 8 options.Timeout = TransactionManager.DefaultTimeout; 9 10 using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options)) 11 { 12 //数据操作第一步; 13 //数据操作第二步; 14 //数据操作第三步; 15 scope.Complete(); 16 17 } 18 }这样做的好处就是事务的好处,避免做了第一步数据操作而第二步或者第三不没有做造成数据的不统一。 最后提醒下:要添加引用并using System.Transactions;
|