开发者

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.Store开发者_运维百科dProcedure;
                       ...
                        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.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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜