SubSonic 事务 - 使用第一个表的返回标识插入到第二个表

发布于 2024-08-15 02:08:45 字数 858 浏览 6 评论 0原文

我使用下面的代码使用第一个表 (info2.Id = info.Id;) 中使用的标识更新第二个表 (Info2)。当执行第二次保存(info2.Save())时,我收到错误:“已经有一个与此命令关联的打开的 DataReader,必须首先关闭它。”。 谁能看到我可能做错了什么。

SubSonic 版本 3.0.0.3 和 SQL Server 2005

谢谢

                using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        Info info = new Info();
                        info.Desc = "Some information";
                        info.Save();

                        Info2 info2 = new Info2();
                        info2.Id = info.Id;
                        info2.Desc = "More information";
                        info2.Save();

                        ts.Complete();
                    }
                }

I am using the code below to update a second table (Info2) with the identity used from the first table (info2.Id = info.Id;). When the second save is carried out (info2.Save()) I get the error: "There is already an open DataReader associated with this Command which must be closed first.".
Can anyone see what I may be doing wrong.

SubSonic version 3.0.0.3 and SQL Server 2005

Thanks

                using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        Info info = new Info();
                        info.Desc = "Some information";
                        info.Save();

                        Info2 info2 = new Info2();
                        info2.Id = info.Id;
                        info2.Desc = "More information";
                        info2.Save();

                        ts.Complete();
                    }
                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蘸点软妹酱 2024-08-22 02:08:45

看起来您的 TransactionScope 和 SharedDbConnectionScope 的方向是错误的,请尝试:

using (TransactionScope ts = new TransactionScope())
{
  using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())
  {
    Info info = new Info();
    info.Desc = "Some information";
    info.Save();

    Info2 info2 = new Info2();
    info2.Id = info.Id;
    info2.Desc = "More information";
    info2.Save();

    ts.Complete();
  }
}

Looks like you've got the TransactionScope and the SharedDbConnectionScope the wrong way round, try:

using (TransactionScope ts = new TransactionScope())
{
  using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())
  {
    Info info = new Info();
    info.Desc = "Some information";
    info.Save();

    Info2 info2 = new Info2();
    info2.Id = info.Id;
    info2.Desc = "More information";
    info2.Save();

    ts.Complete();
  }
}
笨死的猪 2024-08-22 02:08:45

根据我在评论中所写的内容,我可以实现此功能的唯一方法是首先使用 TransactionScope,然后使用 SharedDbConnectionScope (感谢 Adam)并添加 MultipleActiveResultSets=True; (SQL Server 2005) 到连接字符串。

有人有更好的解决方案或其他建议吗?
谢谢

        try
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (SharedDbConnectionScope scs = new SharedDbConnectionScope())
                {
                    Info info = new Info();
                    info.Desc = "Some information";
                    info.Save();

                    //Test for rollback
                    //throw new Exception("STOP");

                    Info2 info2 = new Info2();
                    info2.Id = info.Id;
                    info2.Desc = "More information";
                    info2.Save();

                    ts.Complete();
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

As per what I have written in the comments, the only way I can get this working is using TransactionScope first then SharedDbConnectionScope (thanks Adam) and to add MultipleActiveResultSets=True; (SQL Server 2005) to the connection string.

Anybody have any better solutions or other suggestions?
Thanks

        try
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (SharedDbConnectionScope scs = new SharedDbConnectionScope())
                {
                    Info info = new Info();
                    info.Desc = "Some information";
                    info.Save();

                    //Test for rollback
                    //throw new Exception("STOP");

                    Info2 info2 = new Info2();
                    info2.Id = info.Id;
                    info2.Desc = "More information";
                    info2.Save();

                    ts.Complete();
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文