asp.Net TransactionScope的错误 [英] asp.Net TransactionScope error

查看:104
本文介绍了asp.Net TransactionScope的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的编码

    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 is a class and there are save and update document methods there.

If I comment the transactionScope, I get no errors.

怎么了?

推荐答案

那么,它显然是说什么是错在这里。你需要外界把你的访问层或提​​交调用中使用的连接。当试图提交跨在code,它被设置到达那里之前。

Well, it is clearly saying what is wrong here. You need to put your access layer outside or commit call inside to use connection. When it is trying to commit trans in your code, it is disposed before getting there.

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

}

这篇关于asp.Net TransactionScope的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆