views:

36

answers:

2

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;}

+2  A: 

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.Current to get a handler to the current transaction manager to ensure this.

Note that one TransactionScope object can have óne implicit transaction type, so you will need to nest TransactionScope objects to ensure transactional processing over multiple managers. F.e.:

using(var ts = new TransactionScope)
{
      using(var db = new TransactionScope)
      {
         //do db processing
      }
      using(var msmq = new TransactionScope)
      {
         //do msmq processing
      }
      ts.Complete();
}
Jan Jongboom