2016-12-30 133 views
0

我無法將數據插入到數據庫中,也不會收到錯誤消息。我通過聲明patientNRIC作爲外鍵鏈接表patientRegisterpatientAdmission。以下是代碼。無法將數據插入數據庫,沒有顯示錯誤消息

patientNOK.cs

public class patientNOK 
{ 
    public string nokWardClass { get; set; } 
    public DateTime nokAdDT { get; set; } 
    public string nokFName { get; set; } 
    public string nokLName { get; set; } 
    public string nokNRIC { get; set; } 
    public DateTime nokDOB { get; set; } 
    public string nokGender { get; set; } 
    public string nokNationality { get; set; } 
    public string nokRelationship { get; set; } 
    public int nokContactH { get; set; } 
    public int nokContactHP { get; set; } 
    public string nokEmail { get; set; } 
    public string nokAddr1 { get; set; } 
    public string nokAddr2 { get; set; } 
    public string nokState { get; set; } 
    public string nokZIP { get; set; } 
    public string nokCountry { get; set; } 
    public DateTime dischargeDT { get; set; } 
    public string patientNRICForiegn { get; set; } 
} 

public class patientNOKDAO 
{ 
    string DBConnect = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; 

    public int InsertNOK(string wardClass , DateTime admissionDT, string nokFName, string nokLName, string nokNRIC, DateTime nokDOB, string nokGender, string nokNationality, string nokRelationship, int nokContactH, int nokContactHP, string nokEmail, string nokAddr1, string nokAddr2, string nokState, int nokZIP, string nokCountry, DateTime dischargeDT, string patientNRIC) 
    { 
     StringBuilder sqlStr = new StringBuilder(); 
     int result = 0; 
     SqlCommand sqlCmd = new SqlCommand(); 

     sqlStr.AppendLine("Insert INTO patientAdmission(wardClass, admissionDT, patientNokFname, patientNokLname, patientNokNRIC, patientNOKDOB"); 

     sqlStr.AppendLine("patientNokGender, patientNokNationality, patientNokRelationship, patientNokContactH, patientNokContactHP, patientNokEmail"); 

     sqlStr.AppendLine("patientNokAddr1, patientNokAddr2, patientNokZIP, patientNokCountry, dischargeDateTime,patientNRIC)"); 

     sqlStr.AppendLine("VALUES (@parawardClass,@paraadmissonDT,@parapatientNokFname,@parapatientNokLname,@parapatientNokNRIC, @parapatientNOKDOB,@parapatientNokGender,@parapatientNokNationality"); 
     sqlStr.AppendLine("@parapatientNokRelationship,@parapatientNokContactH,@parapatientNokContactHP,@parapatientNokEmail,@parapatientNokAddr1,@parapatientNokAddr2,@parapatientNokState,@parapatientNokZIP"); 
     sqlStr.AppendLine("@parapatientNokCountry,@paradischargeDateTime,@parapatientNRIC)"); 

     try 
     { 
      SqlConnection myConn = new SqlConnection(DBConnect); 
      sqlCmd = new SqlCommand(sqlStr.ToString(), myConn); 

      sqlCmd.Parameters.AddWithValue("@parawardClass", wardClass); 
      sqlCmd.Parameters.AddWithValue("@paraadmissionDT", admissionDT); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokFname", nokFName); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokLname", nokLName); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokNRIC", nokNRIC); 
      sqlCmd.Parameters.AddWithValue("@parapatientNOKDOB", nokDOB); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokGender", nokGender); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokNationality", nokNationality); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokRelationship", nokRelationship); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokContactH", nokContactH); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokContactHP", nokContactHP); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokEmail", nokEmail); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokAddr1", nokAddr1); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokAddr2", nokAddr2); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokState", nokState); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokZIP", nokZIP); 
      sqlCmd.Parameters.AddWithValue("@parapatientNokCountry", nokCountry); 
      sqlCmd.Parameters.AddWithValue("@paradischargeDateTime", dischargeDT); 
      sqlCmd.Parameters.AddWithValue("@parapatientNRIC", patientNRIC); 

      myConn.Open(); 
      result = sqlCmd.ExecuteNonQuery(); 

      myConn.Close(); 
     } 
     catch(Exception ex) 
     { 
      logManager log = new logManager(); 
      log.addLog("patientNOKDAO.InsertNOK", sqlStr.ToString(), ex); 
     } 
     return result; 
    } 
} 

UI

string wardPreference = ddl1.SelectedValue; 

     string admissionDischargeDT = adDT.Text; 
     string admissionDT = admissionDischargeDT.Substring(0,19); 
     string dischargeDT = admissionDischargeDT.Substring(22,19); 

     DateTime admissionDTinput = DateTime.ParseExact(admissionDT,"MM/dd/yyyy hh:mm tt",System.Globalization.CultureInfo.InvariantCulture); 

     DateTime dischargeDTinput = DateTime.ParseExact(dischargeDT, "MM/dd/yyyy hh:mm tt", System.Globalization.CultureInfo.InvariantCulture); 

     string FNameNOK = TextBoxNOKFname.Text; 
     string LNameNOK = TextBoxNOKLname.Text; 
     string nricNOK = TextBoxNOKNRIC.Text; 
     // 
     DateTime dobNOK = DateTime.ParseExact(nokDOB.Text.ToString(), "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture); 
     string selectedNOKGender = null; 
     if(Request.Form["radio-unstyled-inline-left"] !=null) 
     { 
      selectedNOKGender = Request.Form["radio-unstyled-inline-left"].ToString(); 
     } 

     string nationalityNok = TextBoxNOKNationality.Text; 
     string relationshipNOK = DropDownListRelationship.SelectedValue; 

     int nokContactH = Convert.ToInt32(TextBoxNOKContactH.Text); 
     int nokContactHP = Convert.ToInt32(TextBoxNOKContactHP.Text); 
     string nokEmail = TextBoxNOKContactEmail.Text; 
     string nokAddr1 = TextBoxNOKAddr1.Text; 
     string nokAddr2 = TextBoxNOKAddr2.Text; 
     string nokState = TextBoxNOKState.Text; 
     int nokZIP = Convert.ToInt32(TextBoxNOKZIP.Text); 
     string nokCountry = TextBoxNOKCountry.Text; 
     string patientNRIC = TextBoxNRIC.Text; 

     try 
     { 
      patientNOKDAO fmNOK = new patientNOKDAO(); 

      int insNOK = fmNOK.InsertNOK(wardPreference, admissionDTinput, FNameNOK, LNameNOK, nricNOK, dobNOK, selectedNOKGender, nationalityNok, relationshipNOK, nokContactH, nokContactHP, nokEmail, nokAddr1, nokAddr2, nokState, nokZIP, nokCountry, dischargeDTinput, patientNRIC); 

      if (insNOK == 1) 
      { 
       TextBoxAddr1.Text = "Sucess"; 
      } 
      else 
      { 
       TextBoxAddr1.Text = "FAILURE"; 
      } 
     } 
     catch (FormatException) 
     { 
      TextBoxAddr1.Text = "error"; 
     } 
    } 

我試圖查看它顯示了這個日誌文件,顯然有在NOK類

問題

異常類型:System.Data.SqlClient.SqlExcep例外: 'patientNokGender'附近語法不正確。來源: patientNOKDAO.InsertNOK SQL命令:INSERT INTO patientAdmission( wardClass,admissionDT,patientNokFname,patientNokLname, patientNokNRIC,patientNOKDOB patientNokGender,patientNokNationality, patientNokRelationship,patientNokContactH,patientNokContactHP, patientNokEmail patientNokAddr1,patientNokAddr2,patientNokZIP, patientNokCountry,dischargeDateTime, patientNRIC)VALUES (@ parawardClass,@ paraadmissonDT,@ parapatientNokFname,@ parapatientNokLname,@ parapatientNokNRIC, @ parapatientNOKDOB,@ parapatientNokGender,@ parapatientNokNationality @ parapatientNokRelationship,@ parapatientNokContactH,@ parapatientNokContactHP,@ parapatientNokEmail,@ parapatientNokAddr1,@ parapatientNokAddr2,@ parapatientNokStat即,@ parapatientNokZIP @ parapatientNokCountry,@ paradischargeDateTime,@ parapatientNRIC)

+0

嘗試調試並檢查日誌。 –

+0

執行代碼時'result'的值是多少? – Mahdi

回答

0
可以

ü請測試最終生成的SQL命令。 在這種類型的問題我運行sqlCmd在一個新的查詢在Microsoft SQL Server中。

+0

嗨,我該怎麼做呢? –

+0

親愛的@ GX-X請在該行插入一個斷點結果=您的sqlCmnd並運行您的應用程序。然後複製sqlCmd的值並將其粘貼到新的SQL查詢窗口中並運行它。 –

0

看起來你錯過在某些點逗號在字符串連接,第一次是在這裏:

patientNOKDOB patientNokGender

另一個是在這裏:

patientNokEmail patientNokAddr1

這裏是原因:

sqlStr.AppendLine("Insert INTO patientAdmission(wardClass, admissionDT, patientNokFname, patientNokLname, patientNokNRIC, patientNOKDOB"); 

sqlStr.AppendLine("patientNokGender, patientNokNationality, patientNokRelationship, patientNokContactH, patientNokContactHP, patientNokEmail"); 

你還需要確保你添加逗號值字符串連接爲好。

0

更新您的以下線 -

sqlStr.AppendLine("Insert INTO patientAdmission(wardClass, admissionDT, patientNokFname, patientNokLname, patientNokNRIC, patientNOKDOB"); 
sqlStr.AppendLine("patientNokGender, patientNokNationality, patientNokRelationship, patientNokContactH, patientNokContactHP, patientNokEmail"); 
sqlStr.AppendLine("patientNokAddr1, patientNokAddr2, patientNokZIP, patientNokCountry, dischargeDateTime,patientNRIC)"); 
sqlStr.AppendLine("VALUES (@parawardClass,@paraadmissonDT,@parapatientNokFname,@parapatientNokLname,@parapatientNokNRIC, @parapatientNOKDOB,@parapatientNokGender,@parapatientNokNationality"); 
sqlStr.AppendLine("@parapatientNokRelationship,@parapatientNokContactH,@parapatientNokContactHP,@parapatientNokEmail,@parapatientNokAddr1,@parapatientNokAddr2,@parapatientNokState,@parapatientNokZIP"); 
sqlStr.AppendLine("@parapatientNokCountry,@paradischargeDateTime,@parapatientNRIC)"); 

sqlStr.AppendLine("Insert INTO patientAdmission(wardClass, admissionDT, patientNokFname, patientNokLname, patientNokNRIC, patientNOKDOB, "); 
sqlStr.AppendLine("patientNokGender, patientNokNationality, patientNokRelationship, patientNokContactH, patientNokContactHP, patientNokEmail, "); 
sqlStr.AppendLine("patientNokAddr1, patientNokAddr2, patientNokZIP, patientNokCountry, dischargeDateTime,patientNRIC)"); 
sqlStr.AppendLine("VALUES (@parawardClass,@paraadmissonDT,@parapatientNokFname,@parapatientNokLname,@parapatientNokNRIC, @parapatientNOKDOB,@parapatientNokGender,@parapatientNokNationality, "); 
sqlStr.AppendLine("@parapatientNokRelationship,@parapatientNokContactH,@parapatientNokContactHP,@parapatientNokEmail,@parapatientNokAddr1,@parapatientNokAddr2,@parapatientNokState,@parapatientNokZIP, "); 
sqlStr.AppendLine("@parapatientNokCountry,@paradischargeDateTime,@parapatientNRIC)"); 

剛剛追加 「」 你的第一,第二,第四和第五行進行適當的SQL生成。

相關問題