2016-12-30 64 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生成。