Comment vérifier si la transaction Dotnet est annulée?
-
10-10-2019 - |
Question
Comment puis-je vérifier si une transaction dotnet est fermée ou non?
La solution
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)
{
}
}
Autres conseils
Votre titre demande une chose et votre question demande à un autre. donc, je vais avec votre titre.
Si vous voulez savoir si la transaction est annulée ou un ensemble rollback seulement, vous pouvez vérifier
transaction.WasRolledBack // true if transaction is rolled back
Ici, transaction
est une instance de ITransaction
Modifier (en fonction de votre commentaire) :
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;
}
}
}
Maintenant, vous pouvez vérifier le drapeau isRolledBack
pour voir si la transaction est annulée
Si vous êtes sur le serveur SQL, vous pouvez utiliser DBCC OPENTRAN
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow