【问题标题】:Insert with Linq-to-SQL sometimes fails使用 Linq-to-SQL 插入有时会失败
【发布时间】:2012-03-05 10:25:07
【问题描述】:

我有一个项目,将个人信息插入到一个表中,并将详细信息插入到另一个表中。但有时无法记录个人信息,但会记录详细信息。如下代码部分,首先插入个人信息,然后是详细信息。但有时个人信息没有被保存,userId 返回 0,所以细节被保存了。我不知道为什么它不起作用。有什么想法吗?

 public int ConferenceIdyeGoreKisiBilgileriniKaydet(string orderId)
 {
        KisiselBilgilerBal kisiBilgileri = (KisiselBilgilerBal)Session["kisiselBilgilerSession"];
        registrationCode = GenerateGeristrationCode();
        string toplamMaliyet = Session["toplamOdeme"].ToString();
        PersonalInformation.SavePersonalInformations(kisiBilgileri,  registrationCode,conferenceName);
        int userId = AuthorPaperDetaylari.AdVeSoyadaGoreIdGetir(kisiBilgileri.f_name, kisiBilgileri.l_name);
        AuthorPaperDetaylari.SaveAuthorPaperDetails(authorPaperDetay, userId); // save details via userId.

        return userId;
    }

此方法保存个人信息。

public static void SavePersonalInformations(KisiselBilgilerBal kisiBilgileri,string  registrationCode,string conferenceName)
{
        try
        {
            string cs = ConfigurationManager.AppSettings["SiteSqlServer"];
            DBDataContext db = new DBDataContext(cs);
            DBpersonalInformation personalInfo = new DBpersonalInformation();
            personalInfo.f_name = kisiBilgileri.f_name;
            personalInfo.l_name = kisiBilgileri.l_name;
            personalInfo.university_affiliation = kisiBilgileri.university_affiliation;
            personalInfo.department_name = kisiBilgileri.department_name;
            personalInfo.address1 = kisiBilgileri.address1;
            personalInfo.address2 = kisiBilgileri.address2;
            personalInfo.city = kisiBilgileri.city;
            personalInfo.state = kisiBilgileri.state;
            personalInfo.zipCode = kisiBilgileri.zipCode;
            personalInfo.country = kisiBilgileri.country;
            personalInfo.phone = kisiBilgileri.phone;
            personalInfo.email = kisiBilgileri.email;
            personalInfo.orderId = kisiBilgileri.orderId;
            personalInfo.registrationCode = registrationCode;
            personalInfo.date = DateTime.Now;
            personalInfo.conferenceName = conferenceName;
            db.DBpersonalInformations.InsertOnSubmit(personalInfo);
            db.SubmitChanges();
        }
        catch (Exception)
        {
        }
    }

此方法保存细节

public static void SaveAuthorPaperDetails(AuthorPaperDetailsBal authorPaperDetay, int userId)
{
        try
        {
            string cs = ConfigurationManager.AppSettings["SiteSqlServer"];

            DBWebDataContext db = new DBWebDataContext(cs);

            DBAuthorPaperDetail authorPaperDetail = new DBAuthorPaperDetail();

            authorPaperDetail.paper_title = authorPaperDetay.paperTitleDetails;
            authorPaperDetail.conference_maker_id = authorPaperDetay.confMakerId;
            authorPaperDetail.additional_paper_title = authorPaperDetay.additionalPprTtle;
            authorPaperDetail.areYouMainAuthor = authorPaperDetay.mainAuthor;
            authorPaperDetail.feeForFirstAuthorPaper = authorPaperDetay.registerFeeForFirstAuthor;
            authorPaperDetail.feeForAdditionalPaper = authorPaperDetay.regFeeForAdditionalPape;
            authorPaperDetail.feeForParticipCoAuthors = authorPaperDetay.regFeeForCoAuthors;
            authorPaperDetail.userId = userId;
            authorPaperDetail.firstCoAuthorName = authorPaperDetay.firstCoAuthor;
            authorPaperDetail.secondCoAuthorName = authorPaperDetay.secondCoAutho;
            authorPaperDetail.thirdCoAuthorName = authorPaperDetay.thirdCoAuthor;
            authorPaperDetail.toplamOdeme = authorPaperDetay.toplamMaliyet;
            db.DBAuthorPaperDetails.InsertOnSubmit(authorPaperDetail);
            db.SubmitChanges();
        }
        catch (Exception)
        {
        }
    }

【问题讨论】:

    标签: c# database linq linq-to-sql


    【解决方案1】:

    我不知道为什么它不起作用。有什么想法吗?

    ...

    catch (Exception)
    {
    
    }
    

    嗯,这几乎解释了一切……不要这样做。曾经。数据库层试图告诉您问题出在哪里,而您正把手指放在耳朵里,希望它会消失。如果我不得不猜测:可能是由于被另一个 SPID 阻止而导致偶尔超时。

    如果你不能做任何有用或适当的异常,就让它冒泡给调用者。如果它到达 UI,告诉用户它(或者只是在内部记录问题并告诉用户“有一个问题”)。

    另外,LINQ-to-SQL 数据上下文是IDisposable;你应该在db 周围有using 声明。

    【讨论】:

      【解决方案2】:

      除了 Marc 的回答...您调用了 SubmitChanges 两次。如果你想要原子数据存储,你应该调用一次。您可以使用关系属性创建对象图,并一次提交整个图。

      public void SaveParentAndChildren()
      {
        using (CustomDataContext myDC = new CustomDataContext())
        {
          Parent p = new Parent();
          Child c = new Child();
          p.Children.Add(c);
          myDC.Parents.InsertOnSubmit(p); //whole graph is now tracked by this data context
          myDC.SubmitChanges(); // whole graph is now saved to database
          // or nothing saved if an exception occurred.
      
        }  //myDC.Dispose is called for you here whether exception occurred or not
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多