如何检查Dotnet事务是否回滚?

如何检查dotnet交易是否已关闭?

using(TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew)){ try{ //Do something; scope.Complete(); //denotes the transaction completed successful. } catch(TransactionAbortedException ex) { //scope.Complete(); is never called, the transaction rolls back automatically. } catch(ApplicationException ex) { } } 

你的标题问了一件事,你的问题又问了另一件事。 所以,我想要你的头衔。

如果您想知道事务是否已回滚或仅设置为回滚,则可以检查

 transaction.WasRolledBack // true if transaction is rolled back 

在这里, transaction是ITransaction的一个实例

编辑(根据您的评论)

 var isRolledBack = false; using (var connection = new SqlConnection()) { using (var transaction = connection.BeginTransaction()) { try { // do your stuff here with transaction } catch (Exception ex) { transaction.Rollback(); isRolledBack = true; throw; } } } 

现在,您可以检查isRolledBack标志以查看事务是否已回滚

如果您在SQL Server上,则可以使用DBCC OPENTRAN

http://msdn.microsoft.com/en-us/library/ms182792.aspx