简体   繁体   English

语句中查询表达式中的语法错误(缺少运算符)

[英]Syntax error (missing operator) in query expression in statement

     sqlStatement = "select Price from OrderItem where orderId=" + orderId;
            myAccessCommand = new OleDbCommand(sqlStatement, myAccessConn);
            myDataAdapter = new OleDbDataAdapter(myAccessCommand);
            myDataAdapter.Fill(myDataSet, "OrderItem");`DataTableCollection dta = myDataSet.Tables;
            DataColumnCollection drc = myDataSet.Tables["Orders"].Columns;
            DataRowCollection dra = myDataSet.Tables["Orders"].Rows;

            foreach (DataRow dr in dra)
            {
                orderId= dr[0].ToString();
                Checkintime= dr[1].ToString();
                RoomPrice= dr[2].ToString();
                ReceiptNo= dr[3].ToString();
                Console.WriteLine("orderId: " + orderId + ", Checkintime:  " + Checkintime + ", RoomPrice: " + RoomPrice + ", ReceiptNo: " + ReceiptNo + "");

            }`

I have a syntax error which says "Syntax error (missing operator) in query expression 'orderId='."} 我有一个语法错误,其中显示“查询表达式'orderId ='中的语法错误(缺少运算符)。”}

I cant seem to find the error. 我似乎找不到错误。

The root cause of your problem is that you are not using parameterized queries and are trying to create an sql string on the fly. 问题的根本原因是您没有使用参数化查询,而是试图动态创建sql字符串。 As a result you make an error in the assembling code of that string. 结果,您在该字符串的汇编代码中出错。 But if you use a parameterized query the chance of running into an issue like that is a lot lower because you don't have to mess about with quotes, number to string conversions and the like. 但是,如果您使用参数化查询,则遇到类似问题的机会会大大降低,因为您不必担心引号,数字到字符串的转换等问题。 On top of this, you cannot have a sql injection attack if you use parameters and it makes the code more readable too. 最重要的是,如果使用参数,则不会受到sql注入攻击,这也会使代码更具可读性。

Read http://www.dotnetperls.com/sqlparameter on how to use a parameterized query the way it should be done and don't just fix the textual error in the querystring. 阅读http://www.dotnetperls.com/sqlparameter ,了解如何使用参数化查询应采用的方式,而不仅仅是解决查询字符串中的文本错误。 It is not the way it is supposed to be done. 这不是应该完成的方式。

This is a good explanation too : http://www.dreamincode.net/forums/topic/268104-parameterizing-your-sql-queries-the-right-way-to-query-a-database/ 这也是一个很好的解释: http : //www.dreamincode.net/forums/topic/268104-parameterizing-your-sql-queries-the-right-way-to-query-a-database/

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

相关问题 查询表达式,Oledb UPDATE语句中的语法错误(缺少运算符) - Syntax error (missing operator) in query expression, Oledb UPDATE Statement 查询表达式Oledb INSERT语句中的语法错误(缺少运算符) - Syntax error (missing operator) in query expression, Oledb INSERT Statement 查询表达式“”中的语法错误(缺少运算符) - Syntax error (missing operator) in query expression “” 查询表达式中的语法错误(缺少运算符) - Syntax error (missing operator) in query expression 查询表达式'[Code] IN('中的语法错误(缺少运算符) - Syntax error (missing operator) in query expression '[Code] IN(' 查询表达式中缺少语法错误运算符 - syntax error missing operator in query expression sql查询表达式中的语法错误(缺少运算符) - syntax error (missing operator) in sql query expression 获取异常错误说:查询表达式中的语法错误(缺少运算符) - Getting Exception error saying: Syntax error (missing operator) in query expression 查询表达式中的C#语法错误(缺少运算符) - C# Syntax error (missing operator) in query expression OleDbException未处理:查询表达式中的语法错误(缺少运算符) - OleDbException was unhandled: Syntax Error (missing operator) In Query Expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM