简体   繁体   English

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

[英]Insert with Linq-to-SQL sometimes fails

I have a project that inserts personal information to a table and details into another table. 我有一个项目,将个人信息插入表格,并将详细信息插入另一个表格。 But sometimes personal information cannot be recorded, however details are recorded. 但有时无法记录个人信息,但会记录细节。 As below code part, firstly personal information are inserted, then details. 如下面的代码部分,首先插入个人信息,然后插入详细信息。 But sometimes personal information doesn't get saved and userId returns 0, So details are saved. 但有时个人信息不会被保存, userId返回0,因此会保存详细信息。 I don't know why it doesn't work. 我不知道为什么它不起作用。 Any idea? 任何想法?

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

This method saves personal information. 此方法可以保存个人信息。

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)
        {
        }
    }

This method saves details 此方法保存详细信息

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)
        {
        }
    }

I don't know why it doesnt work. 我不知道为什么它不起作用。 Any idea? 任何想法?

... ...

catch (Exception)
{

}

Well, that explains pretty much everything... don't do this. 好吧,这几乎解释了所有事情......不要这样做。 Ever. 永远。 The database layer is trying to tell you what the problem is, and you are sticking your fingers in your ears, hoping that'll make it go away. 数据库层试图告诉你问题是什么,你的手指在你的耳朵里,希望它会让它消失。 If I had to guess: maybe an occasional timeout due to being blocked by another SPID. 如果我不得不猜测:由于被另一个SPID阻止,可能偶尔会超时。

If you can't do anything useful or appropriate with an exception, just let it bubble to the caller. 如果你不能做任何有用或适当的例外,只需让它冒泡到调用者。 If it gets to the UI, tell the user about it (or just log the issue internally and tell the user "There was a problem"). 如果它到达UI,告诉用户它(或者只是在内部记录问题并告诉用户“出现问题”)。

Also, a LINQ-to-SQL data-context is IDisposable ; 此外,LINQ-to-SQL数据上下文是IDisposable ; you should have using statement around db . 你应该using db周围的语句。

In addition to Marc's answer... You are calling SubmitChanges twice. 除了Marc的答案之外......你要两次调用SubmitChanges。 If you want atomic data storage, you should call it once. 如果您想要原子数据存储,则应该调用一次。 You can use relational properties to create an object graph, and submit the whole graph at once. 您可以使用关系属性来创建对象图,并立即提交整个图。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM