最近在開發(fā)過程中用到比較多的批處理讓我感覺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 //數(shù)據(jù)操作第一步; 13 //數(shù)據(jù)操作第二步; 14 //數(shù)據(jù)操作第三步; 15 scope.Complete(); 16 17 } 18 }這樣做的好處就是事務的好處,避免做了第一步數(shù)據(jù)操作而第二步或者第三不沒有做造成數(shù)據(jù)的不統(tǒng)一。 最后提醒下:要添加引用并using System.Transactions;
|