Omu
Omu

Reputation: 71198

is TransactionScope usable for sql operations, is it doing the same thing as Sqltransaction?

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

Upvotes: 0

Views: 151

Answers (1)

Jan Jongboom
Jan Jongboom

Reputation: 27313

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

Upvotes: 2

Related Questions