使用Linq-to-SQL插入有时会失败

时间:2012-03-05 10:25:08

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

我有一个项目,可以将个人信息插入表格,并将详细信息插入另一个表格。但有时无法记录个人信息,但会记录细节。如下面的代码部分,首先插入个人信息,然后插入详细信息。但有时个人信息不会被保存,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)
        {
        }
    }

2 个答案:

答案 0 :(得分:8)

  

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

...

catch (Exception)
{

}

嗯,这几乎解释了所有事情......不要这样做。永远。数据库层试图告诉你问题是什么,而且你的手指紧贴着你的耳朵,希望它会让它消失。如果我不得不猜测:可能是由于被另一个SPID阻止而偶尔会超时。

如果你不能做任何有用或适当的例外,只需让它冒泡到调用者。如果它进入用户界面,告诉用户它(或者只是在内部记录问题并告诉用户“出现问题”)。

此外,LINQ-to-SQL数据上下文为IDisposable;你应该在using附近发表db声明。

答案 1 :(得分: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
}