简体   繁体   English

如何从数据库获取值

[英]How can I get values from database

I tried to get values from access data base with two where clause. 我试图通过两个where子句从访问数据库中获取值。 This is the error that I got! 这是我得到的错误! "Syntax error (missing operator) in query expression 'unit1<=34 and unit2>=34 where"' . "Syntax error (missing operator) in query expression 'unit1<=34 and unit2>=34 where"'

and this is my code: 这是我的代码:

OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\Work\\Office\\Electricity_Board_bill_calculator\\gk.accdb;");
con.Open(); 
OleDbCommand com5 = new OleDbCommand("select id from tblBillConfig where  unit1<=" 
          + contot + " and unit2>=" + contot + " where group=3 ", con);

You have 'where' in 2 places of the SQL string. 您在SQL字符串的2个位置中有“ where”。 This is at least one reason for the error. 这至少是导致错误的原因之一。

There are a couple of potential issues: 有两个潜在的问题:

  • You can't have 2 where clauses. 您不能有2个where子句。 The second filter needs to be introduced with and` 第二个过滤器需要与和一起引入
  • Group is a reserved keyword, so and needs to be escaped. Group是保留关键字,因此需要转义。 (This would be [group] in Sql Server. I'm not sure how to do this in MS Access) (这将是Sql Server中的[group] 。我不确定如何在MS Access中执行此操作)

You should also look at using parameters to bind variables . 您还应该查看使用参数绑定变量 This addresses a bunch of issues, such as sql injection, and also improves performance as the parameterization may allow your RDBMS to cache the query plan. 这解决了很多问题,例如sql注入,并且还提高了性能,因为参数化可能允许您的RDBMS缓存查询计划。

So your query should look something like this: 因此,您的查询应如下所示:

var com5 = new OleDbCommand("select id from tblBillConfig " +
                            " where unit1<=? and unit2>= ? and [group]=3 ", con);
command.Parameters.Add("@p1", OleDbType.Integer).Value = 34;
command.Parameters.Add("@p2", OleDbType.Integer).Value = 34;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM