[英]multiple Parameters - Syntax error in string in query expression: VS 2010 with MS-Access 2003
我正在接收
OleDBException是查询中“语法错误(缺少运算符)”的未处理错误
表达式'((StudentID = 100'或StudentName ='Nick'或StudentCNCI ='78894452)Bob'。”
private void btnFind_Click(object sender, EventArgs e)
{
string title = textBox1.Text.ToString();
string queryString = "SELECT * FROM Students WHERE (StudentID = " + StudIDTb.Text.ToString() + "' OR StudentName = '" + StudNameTb.Text.ToString() + "' OR StudentCNCI = '" + StudCNCITb.Text.ToString() + ")" + title;
OleDbCommand command = new OleDbCommand();
command.CommandText = queryString;
command.Connection = myCon;
myCon.Open();
OleDbDataReader dr = command.ExecuteReader(); // error pointing here
while (dr.Read())
{
StudIDTb.Text += String.Format("StudentID: {0}\n", dr["StudentID"].ToString());
StudNameTb.Text += String.Format("StudentName: {0}\n", dr["StudentName"].ToString());
StudCNCITb.Text += String.Format("StudentCNIC: {0}\n", dr["StudentCNIC"].ToString());
StudDOBTb.Text += String.Format("StudentDOB: {0}\n", dr["StudentDOB"].ToString());
}
myCon.Close();
}
我也尝试过...
string queryString = "SELECT * FROM Students WHERE (StudentID = " + StudIDTb.Text + "' OR StudentName = '" + StudNameTb.Text + "' OR StudentCNCI = '" + StudCNCITb.Text + ")" + title;
我不想给您错误的印象,我“很懒”,但我假设我收到此错误,因为我查询不正确或输入错误,或者可能是其他错误。 请有人可以帮助我,在此先感谢。
ps我知道我因不使用参数化查询而受到批评。 一旦获得基本权利,我将对其进行更改。 我知道这里曾问过许多类似的问题,但我仍然做不清楚。
更新1我已将其更改为
"SELECT * FROM Students WHERE StudentID = " + StudIDTb.Text + " OR StudentName = '" + StudNameTb.Text + "', OR StudentCNCI = '" + StudCNCITb.Text + ")";
我现在收到以下错误:
查询表达式中的语法错误(逗号)
我正在调查
更新2
string queryString = "SELECT * FROM Students WHERE StudentID = " + StudIDTb.Text + "' OR StudentName = '" + StudNameTb.Text + "' OR StudentCNCI = '" + StudCNCITb.Text + "'";
收到相同的错误。
看着它
更新3:如果无法解决,请按照应有的方式进行,强烈建议使用参数化查询,以解决问题,并且可能容易发现代码中的任何问题
它告诉您查询无效。 你有这个
SELECT *
FROM Students
WHERE (StudentID='a' OR StudentName='b' or StudentCNCI='c')Bob
最终鲍勃并不喜欢鲍勃,也不清楚为什么需要鲍勃。 解释一下您的意图是什么,或者只是摆脱它,因为它对于您的查询似乎不是必需的。
string queryString = "SELECT * FROM Students WHERE StudentID = '" +
StudIDTb.Text + "' OR StudentName = '" + StudNameTb.Text +
"' OR StudentCNCI = '" + StudCNCITb.Text + "'";
正如您在帖子中提到的那样,您还需要参数化查询。 如果您需要帮助,请告诉我们,但这非常简单,并且在此处很常见,因此您已经拥有足够的资源来解决该问题。
编辑:如果您愿意,可以删除括号。 如果要执行子查询或类似的操作,则实际上只需要这样。 它们不会损害您的查询,只是没有必要。
SELECT *
FROM Students
WHERE StudentID='a' OR StudentName='b' or StudentCNCI='c'
同样,从其他注释中,您实际上有多个引用不匹配(一个在开头,另一个在结尾)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.