简体   繁体   English

OleDBException未处理:查询中的语法错误(不完整的查询子句)

[英]OleDBException was unhandled: Syntax error in query (Incomplete query clause)

I'm trying to execute a very simple SQL statement on an Access database through C#. 我正在尝试通过C#在Access数据库上执行一个非常简单的SQL语句。

The statement is something like this: 该语句是这样的:

select M_PASSWORD from TB_USERS where M_USERNAME = 'myuser'

and this is the C# code I'm using to execute the SQL statement: 这是我用来执行SQL语句的C#代码:

string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sources.global_variables.db_source;
using (OleDbConnection connection = new OleDbConnection(connString))
{
    connection.Open();
    OleDbDataReader reader = null;
    OleDbCommand command = new OleDbCommand("SELECT @1 from @2 WHERE @3='@4'", connection);
    command.Parameters.AddWithValue("@1", db_column);
    command.Parameters.AddWithValue("@2", db_table);
    command.Parameters.AddWithValue("@3", db_where_column);
    command.Parameters.AddWithValue("@4", db_where_value);
    reader = command.ExecuteReader();
    //rest of code

Once I get to the line reader = command.ExecuteReader(); 一旦我到达该行reader = command.ExecuteReader(); , the reader fails the execution of the query giving me the following error message: OleDBException was unhandled: Syntax error in query (Incomplete query clause) . ,读取器将无法执行查询,并显示以下错误消息: OleDBException was unhandled: Syntax error in query (Incomplete query clause) I've debugged the code to see if I could see any wrong assignment in the parameters values, but they look fine. 我已经调试了代码,以查看是否可以在参数值中看到任何错误的分配,但是它们看起来不错。

Moreover, executing the exact same query on the Query Analyzer of the Database, I retrieve the value I want. 此外,在数据库的查询分析器上执行完全相同的查询,我检索了所需的值。

Could anyone give a tip to spot the problem and understand where I'm wrong? 谁能给我一个提示以发现问题并了解我的错在哪里?

I think you cant pick column names as parameter such that. 我认为您不能选择列名称作为这样的参数。 It might be the problem. 可能是问题所在。

Use if statement or other conditional statements for parameter and move your query to inside of your conditional statement. 将if语句或其他条件语句用作参数,并将查询移至条件语句内部。

I don't believe that parameters can be used in the fashion you posted. 我认为参数不能以您发布的方式使用。 Parameters are used for filling in values (ie, placing a DateTime value into an update statement as the value of a DateTime column to be updated in a table). 参数用于填充值(即,将DateTime值作为要在表中更新的DateTime列的值放置在update语句中)。

Try changing your code such that the column names and table names are provided in text or are filled in as a string. 尝试更改代码,以使列名和表名以文本形式提供或以字符串形式填充。 You can build the query string up if you want to fill in different column names, different table names, and different column names in your where clause. 如果要在where子句中填写不同的列名,不同的表名和不同的列名,则可以构建查询字符串。 So instead of what you posted, try something more like this: 因此,除了您发布的内容以外,请尝试其他类似的操作:

string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sources.global_variables.db_source;
using (OleDbConnection connection = new OleDbConnection(connString))
{
    connection.Open();
    OleDbDataReader reader = null;
    string strQuery = "SELECT " + constStringColumnName1 + " FROM " + theTableNamePassedInAsString + " WHERE " + strWhereClauseBuiltEarlierInThisFunction + " = '@1'";
    OleDbCommand command = new OleDbCommand( strQuery , connection);
    command.Parameters.AddWithValue("@1", db_where_value);
    reader = command.ExecuteReader();
    //rest of code
}

Of course, you could format the string and plug in your changing selection column name, your table name, and your where clause. 当然,您可以设置字符串格式并插入更改的选择列名称,表名称和where子句。 Build your select/command string, then use Parameters to fill in the actual value is the normal usage. 生成您的选择/命令字符串,然后使用“参数”填写实际值,这是正常用法。

Try remove the ' on where parameter and use ? 尝试删除“ on where参数”并使用? insted of @ like that 像这样的@

OleDbCommand command = new OleDbCommand("SELECT ? from ? WHERE ?=?", connection);
command.Parameters.AddWithValue("column", db_column);
command.Parameters.AddWithValue("table", db_table);
command.Parameters.AddWithValue("where_column", db_where_column);
command.Parameters.AddWithValue("where_value", db_where_value);

I dont know if you can use parameters on column name. 我不知道您是否可以在列名上使用参数。 If it won´t running try to execute the query without parameters using concat and only use parameters on where value 如果无法运行,请尝试使用concat来执行不带参数的查询,而仅在where值上使用参数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OleDbException未处理:查询表达式中的语法错误(缺少运算符) - OleDbException was unhandled: Syntax Error (missing operator) In Query Expression oledbexception是插入语句中未处理的语法错误 - oledbexception was unhandled syntax error in insert into statement C#SQL语句-查询中的语法错误。 不完整的查询子句 - C# SQL statement - Syntax error in query. Incomplete query clause 在C#上运行SQL查询时出错-未处理OleDbException,在SQL语句结束后找到了字符 - Error running SQL query on C# - OleDbException was unhandled, characters found after end of SQL statement oledbException未处理的错误映射 - oledbException was unhandled error appering C# 中的 SQL 查询(System.Data.OleDb.OleDbException:'查询表达式中的语法错误(缺少运算符)) - SQL query in C# (System.Data.OleDb.OleDbException: 'Syntax error (missing operator) in query expression) 消息:System.Data.OleDb.OleDbException:查询表达式中的语法错误(缺少运算符) - Message: System.Data.OleDb.OleDbException : Syntax error (missing operator) in query expression 语句中查询表达式中的语法错误(from子句) - Syntax error (from clause) in query expression in statement 查询FROM子句中的返回语法错误 - Query returning syntax error in FROM clause 错误:System.Data.OleDb.OleDbException (0x80040E14):查询表达式中的语法错误(缺少运算符) - Error: System.Data.OleDb.OleDbException (0x80040E14): Syntax error (missing operator) in query expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM