开发者

SubSonic Transactions - Inserting to a second table with the return identity from first table

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

                usin开发者_JAVA技巧g (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();
                    }
                }


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜