繁体   English   中英

asp.Net TransactionScope错误

[英]asp.Net TransactionScope error

这是我的编码

    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需要一个开放且可用的连接。 连接的当前状态为关闭。

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

If I comment the transactionScope, I get no errors.

怎么了?

好吧,这显然是在说什么地方错了。 您需要将访问层放在外面或在内部提交呼叫才能使用连接。 当它试图在您的代码中提交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();       
   }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM