我一直在尝试将数据更新到数据库但是我遇到了这个问题。我尝试删除表并创建一个新表但问题仍然存在。下面是代码。任何帮助将不胜感激
public int UpdateNOK(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("Update patientAdmission");
sqlStr.AppendLine("SET wardClass = @parawardClass,admissionDT = @paraadmissonDT, nokFName = @parapatientNokFname, nokLName = @parapatientNokLname,nokNRIC = @parapatientNokNRIC, nokDOB = @parapatientNOKDOB, nokGender = @parapatientNokGender, nokNationality = @parapatientNokNationality,");
sqlStr.AppendLine("nokRelationship = @parapatientNokRelationship,nokContactH = @parapatientNokContactH,nokContactHP = @parapatientNokContactHP, nokEmail = @parapatientNokEmail, nokAddr1 = @parapatientNokAddr1,nokAddr2 = @parapatientNokAddr2,nokState = @parapatientNokState, nokZIP = @parapatientNokZIP,");
sqlStr.AppendLine("nokCountry = @parapatientNokCountry, dischargeDT = @paradischargeDateTime");
sqlStr.AppendLine("WHERE patientNRIC = @parapatientNRIC");
try
{
SqlConnection myConn = new SqlConnection(DBConnect);
sqlCmd = new SqlCommand(sqlStr.ToString(), myConn);
sqlCmd.Parameters.AddWithValue("@parawardClass", wardClass);
sqlCmd.Parameters.AddWithValue("@paraadmissonDT", 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();
Console.WriteLine(result);
}
catch (Exception ex)
{
logManager log = new logManager();
log.addLog("patientNOKDAO.UpdateNOK", sqlStr.ToString(), ex);
}
return result;
}
}
答案 0 :(得分:0)
您应该根据表sp_help
中使用过的列检查表定义(patientAdmission
):
wardClass
admissionDT
nokFName
nokLName
nokNRIC
nokDOB
nokGender
nokNationality
nokRelationship
nokContactH
nokContactHP
nokEmail
nokAddr1
nokAddr2
nokState
nokZIP
nokCountry
dischargeDT
patientNRIC
如果数据库默认排序规则区分大小写,则上面的列名必须与表中的定义完全相同(大小写不能相同)。
更快地找到问题的一种方法是运行SQL事件探查器并查看针对数据库的确切查询。从那里复制粘贴并将其运行到Management Studio(SSMS)查询文件中(使用BEGIN TRAN
.. ROLLBACK
以确保在您使其工作时不会实际更改任何内容)。单击错误时,SSMS将尝试指出具有问题的确切列。