[英]Cannot figure out how to fix syntax error (missing operator) in query expression error in C#
I'm having an issue with an error written above and cannot find a exact way to fix it. 我遇到上述错误的问题,无法找到修复它的确切方法。
OleDbDataAdapter dataAdapter = new OleDbDataAdapter("Select count(*) from [contractors$] where " + category + " like '*@name*'", eh.Connection);
dataAdapter.SelectCommand.Parameters.Add("@name", OleDbType.VarChar).Value = "*" + name + "*";
OleDbCommand command = dataAdapter.SelectCommand;
OleDbDataReader reader = command.ExecuteReader();
The exact error is.. 确切的错误是..
Syntax error (missing operator) in query expression 'like ' @name ''. 查询表达式'like'@ name '中的语法错误(缺少运算符)。
I've also already looked for solutions to this problem and have attempted to adapt them to try to get this work work, but with no luck(the one above was one of the attempts) 我也已经在寻找解决这个问题的方法,并试图使它们适应尝试进行这项工作,但是没有运气(上述尝试是其中之一)
Much thanks in advance! 提前非常感谢!
Ok, so I have now change the code to this.. 好的,现在我将代码更改为此。
OleDbDataAdapter dataAdapter = new OleDbDataAdapter("Select count(*) from `contractors$` where " + category + " LIKE @name", eh.Connection);
dataAdapter.SelectCommand.Parameters.Add("@name", OleDbType.VarChar).Value = "%" + name + "%";
OleDbCommand command = dataAdapter.SelectCommand;
OleDbDataReader reader = command.ExecuteReader();
But I am still getting the same error. 但是我仍然遇到同样的错误。
A parameter cannot be contained inside an SQL string literal. 参数不能包含在SQL字符串文字中。 Use concatenation to build the string: 使用串联来构建字符串:
"... LIKE ('%' + @name + '%') ..."
Update 更新资料
It seems that the value of category
was null or empty, creating an invalid SQL statement: 看来category
的值为null或为空,从而创建了无效的SQL语句:
Select count(*) from [contractors$] where like '@name'
^^^ no category here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.