列存在时,即使列存在,也会出现无效的列名

时间:2012-07-31 06:22:22

标签: c# sql

当我尝试使用此查询访问数据库时,它会给我一个错误

invalid column name 'sandeep' which is the name of the patient ie itemArray[3]  

代码:

case "patient":
string queryPatientCheck = @"select * 
                             from patient
                             where 
                             [patientID]={0} and
                             [patientName]={1}";

queryPatientCheck = string.Format(queryPatientCheck, itemArray[2], itemArray[3]);

1 个答案:

答案 0 :(得分:3)

你的sql翻译成这个

select * 
                             from patient
                             where 
                             [patientID]={0} and
                             [patientName]=sandeep

在这种情况下,它会搜索名为sandeep的列。你想要的是字符串sandeep,'sandeep'

select * 
                             from patient
                             where 
                             [patientID]={0} and
                             [patientName]='sandeep'