2011-11-03 87 views

回答

1

你可以在你的交易方法赶上System.Transactions.TransactionException

try 
{ 
    //Create the transaction scope 
    using (TransactionScope scope = new TransactionScope()) 
    { 
     //Register for the transaction completed event for the current transaction 
     Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Current_TransactionCompleted); 
     // proces the transaction 
    } 

} 
catch (System.Transactions.TransactionAbortedException ex) 
{ 
    Console.WriteLine(ex); 
} 
catch (System.Transactions.TransactionException ex) 
{ 
    Console.WriteLine(ex); 
} 
catch 
{ 
    Console.WriteLine("Cannot complete transaction"); 
    throw; 
} 

交易完成事件处理

static void Current_TransactionCompleted(object sender, TransactionEventArgs e) 
{ 
    Console.WriteLine("A transaction has completed:"); 
    Console.WriteLine("ID:    {0}", e.Transaction.TransactionInformation.LocalIdentifier); 
    Console.WriteLine("Distributed ID: {0}", e.Transaction.TransactionInformation.DistributedIdentifier); 
    Console.WriteLine("Status:   {0}", e.Transaction.TransactionInformation.Status); 
    Console.WriteLine("IsolationLevel: {0}", e.Transaction.IsolationLevel); 
} 
+0

谢谢,我的意思是让交易完成事件中的错误信息,因为我只能访问到交易但不能由我的代码来决定是否提交/中止交易。再次感谢。 –

+0

你必须在TransactionScope调用方法上做到这一点。 – Damith

相关问题