【问题标题】:is TransactionScope usable for sql operations, is it doing the same thing as Sqltransaction?TransactionScope 是否可用于 sql 操作,它是否与 Sqltransaction 做同样的事情?
【发布时间】:2010-05-31 09:27:50
【问题描述】:

我可以这样做吗:

using (var scope = new TransactionScope())
            {
                using (var conn = new SqlConnection(Cs))
                {
                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                       ...
                        scope.complete();

                    }
                }
            }

将与使用 catch(){rollback;} 的 SqlTransaction 相同

【问题讨论】:

    标签: c# sql transactionscope


    【解决方案1】:

    TransactionScope 创建一个implicit transaction,因此支持事务处理的事务范围内的任何操作都将由其相应的事务管理器处理。

    在数据库处理的情况下,它将使用SqlTransaction。所以是的,您给定的代码将与使用 SqlTransaction 手动执行相同。


    您可以调用transaction.Current 来获取当前事务管理器的处理程序以确保这一点。

    请注意,一个TransactionScope 对象可以有一个隐式事务类型,因此您需要嵌套TransactionScope 对象以确保在多个管理器上进行事务处理。 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();
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-10
      • 2020-06-10
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 2011-09-01
      相关资源
      最近更新 更多