简体   繁体   English

搜索大于或小于等于的SQL查询时,使用*作为通配符

[英]Use * as a wildcard when searching a SQL query with Greater than or Less than

I am looking to perform a greater than or less than search on multiple columns from an access database in C#. 我希望对C#中的访问数据库中的多列执行大于或小于搜索。

So far I am trying to compare a chassis number value that is stored in a access database against a value in a textbox. 到目前为止,我正在尝试将存储在访问数据库中的机箱号值与文本框中的值进行比较。 If that value is greater than the textbox, this would then return the relevant data stored in the database to a gridview. 如果该值大于文本框,则它将把数据库中存储的相关数据返回到gridview。

so far my code is: 到目前为止,我的代码是:

 var sql = "SELECT * FROM [database] WHERE (Manufacturer ='" + comboBox3.Text +
     "' OR Manufacturer='*') AND (Model ='" + comboBox4.Text + "' OR Model='*') AND (Fuel ='" +
     textBox9.Text + "' OR Fuel='*') AND (Chassisno='*' OR (Chassisno > '" + textBox2.Text + "'))";

The code above is finding results, but the 'greater than' operator is being ignored. 上面的代码正在查找结果,但是“大于”运算符被忽略。

Does anybody have any ideas why this would be? 有谁知道为什么会这样吗?

This portion: 这部分:

Chassisno='*'

Causes the query to find anything. 使查询查找任何内容。 Please remove that part of the query if you are truly only interested in finding values that are greater than Chassisno. 如果您确实只对查找大于Chassisno的值感兴趣,请删除该部分查询。

you can't use * wild card with "=" , you should use "like" keyword : 您不能将*通配符与“ =”一起使用,而应使用“ like”关键字:

.....OR Manufacturer like '*') AND (Model ='" + comboBox4.Text + "' OR Model like '*') AND (Fuel ='" +
 textBox9.Text + "' OR Fuel like '*') AND (Chassisno like'*'.... 

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

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