1

I have a WinForms application that interacts with a SqlCe local database. I manage db operations using BindingSources, TableAdapters and typed datasets.I have several operations scattered through several methods in two different classes that I need to perform in a transaction and I was thinking of using System.Transactions.Transaction or CommitableTransaction. My question is, would Transaction or CommittableTransaction work successfully in a transaction and rollback in an error in this example code or do I have to also use transaction in OtherClass methods too?:

OtherClass othercls = new OtherClass();  
...  

private void DoAll()  
{  
/* begin transaction here */
this.tableAdapter.DoSomeDBWork();  
othercls.DeleteSomeRecords(); //uses tableadapter+sql code to delete. throws exception on error. doesn't have transaction  
othercls.DeletOtherRecords(); //uses tableAdapter.Rows.Find(id).Delete(). throws exception on error. doesn't have transaction  
othercls.Update(); //uses tableadapter.Update(). throws exception on error. doesn't have transaction  
this.DeleteSomeFiles(); //throws exception on fail 
/* end transaction here */ 
}  
4

1 回答 1

1

试试System.Transactions。交易范围

根据 System.Transaction.TransactionScope备注部分,

如果事务范围内确实发生了异常,则它所参与的事务将被回滚

于 2009-03-03T00:48:49.020 回答