简体   繁体   English

无法弄清楚如何在C#中的查询表达式错误中修复语法错误(缺少运算符)

[英]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.

相关问题 查询表达式中的C#语法错误(缺少运算符) - C# Syntax error (missing operator) in query expression C#查询表达式中的语法错误(缺少运算符) - Syntax error (missing operator) in query expression in C# 在C#中的查询表达式中获取语法错误(缺少运算符) - Getting a syntax error (missing operator) in query expression in C# 查询表达式c#中的语法错误(缺少运算符) - Syntax error (missing operator) in query expression c# C# 中的 SQL 查询(System.Data.OleDb.OleDbException:'查询表达式中的语法错误(缺少运算符)) - SQL query in C# (System.Data.OleDb.OleDbException: 'Syntax error (missing operator) in query expression) 在C#中尝试INSERT INTO .accdb获取“查询表达式中的语法错误(缺少运算符)” - Getting “Syntax error (missing operator) in query expression” in C# trying to INSERT INTO .accdb 查询表达式中的C#VS2005语法错误(缺少运算符) - C# VS2005 Syntax error (missing operator) in query expression 查询表达式 '05-04-2014 AM 12:00:00' C# 中的语法错误(缺少运算符)? - Syntax error (missing operator) in query expression '05-04-2014 AM 12:00:00' C#? 使用Access作为数据库的查询表达式C#中缺少语法错误运算符 - syntax error missing operator in query expression c# using access as database 在 C# 中为 MS Access 查询表达式中出现语法错误(缺少运算符) - Getting a syntax error (missing operator) in query expression in C# for MS Access
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM