2012-03-05 76 views
2

我有一個項目可以將個人信息插入到表中,並將細節插入到另一個表中。但有時個人信息不能被記錄,但是細節被記錄下來。如下代碼部分,首先插入個人信息,然後是細節。但是有時候個人信息不會被保存,並且userId返回0,所以細節被保存。我不知道爲什麼它不起作用。任何想法?Linq-to-SQL插入有時會失敗

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

除了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 
} 
8

我不知道爲什麼它不工作。任何想法?

...

catch (Exception) 
{ 

} 

嗯,這解釋了幾乎所有...不這樣做。永遠。數據庫層是試圖告訴你問題是什麼,並且你將手指伸入耳朵,希望這會讓它消失。如果我不得不猜測:由於被另一個SPID阻止,可能會偶爾超時。

如果您不能對異常做任何有用或恰當的事情,只要讓它冒泡給調用者即可。如果它到達用戶界面,告訴用戶它(或者只是在內部登錄問題並告訴用戶「出現問題」)。

此外,LINQ到SQL數據上下文是IDisposable;您應該在db附近有using聲明。