【问题标题】:asp.Net TransactionScope errorasp.Net TransactionScope 错误
【发布时间】:2011-12-12 06:07:26
【问题描述】:

这是我的代码

    using (TransactionScope scope = new TransactionScope())
    {
        using (DataAccess.Document Access = new DataAccess.Document())
        {
            if (toSave.Document.Rows.Count > 0)
            {
                 Access.SaveDocument(docToSave);
            }
            if (toUpdate.Document.Rows.Count > 0)
            {
                 Access.UpdateEachDocument(docToUpdate);
            }
        }
        scope.Complete();
    }

这是错误

ExecuteNonQuery 需要一个开放且可用的连接。连接的当前状态为关闭。

Document 是一个类,里面有保存和更新文档方法。

If I comment the transactionScope, I get no errors.

怎么了?

【问题讨论】:

  • 这里的数据库是什么?您的数据访问代码是什么样的(实际与 DB 对话的位)?
  • 这里需要添加连接代码。
  • 你需要更好地构建问题似乎与stackoverflow.com/questions/8305024/…相同
  • 另外:SQL Server 版本 可能很重要。例如,SQL Server 2000 以与 SQL Server 2008 截然不同的方式使用TransactionScope,例如
  • @kevin 那么老实说,我建议您将其升级给任何确实有权访问的人。如果没有指示性数据访问代码,我们就无法诊断

标签: c# asp.net transactionscope


【解决方案1】:

嗯,它清楚地说明了这里有什么问题。您需要将访问层放在外面或在里面提交调用才能使用连接。当它试图在你的代码中提交 trans 时,它会在到达那里之前被处理掉。

 using (TransactionScope scope = new TransactionScope())
{
    using (DataAccess.Document Access = new DataAccess.Document())
    {
        if (toSave.Document.Rows.Count > 0)
        {
             Access.SaveDocument(docToSave);
        }
        if (toUpdate.Document.Rows.Count > 0)
        {
             Access.UpdateEachDocument(docToUpdate);
        }
      scope.Complete();       
   }

}

【讨论】:

    猜你喜欢
    • 2010-12-10
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    相关资源
    最近更新 更多