can I do something like this:
using (var scope = new TransactionScope())
{
using (var conn = new SqlConnection(Cs))
{
using (var cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
...
scope.complete();
}
}
}
is going to be the same thing as using the SqlTransaction with catch(){rollback;}
TransactionScope creates an
implicit transaction, therefore any action within the transaction scope that supports transactional processing, will be handled by their according Transaction Manager.In the case of database processing, it will use
SqlTransaction. So yes, your given code will do the same as doing it by hand using the SqlTransaction.You can call
transaction.Currentto get a handler to the current transaction manager to ensure this.Note that one
TransactionScopeobject can have óne implicit transaction type, so you will need to nestTransactionScopeobjects to ensure transactional processing over multiple managers. F.e.: