简体   繁体   中英

How can I get values from database

I tried to get values from access data base with two where clause. This is the error that I got! "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. This is at least one reason for the error.

There are a couple of potential issues:

  • You can't have 2 where clauses. The second filter needs to be introduced with and`
  • Group is a reserved keyword, so and needs to be escaped. (This would be [group] in Sql Server. I'm not sure how to do this in 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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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