[英]Query does not return anything
I am trying to get some results from the database, but the query is failing! 我试图从数据库中获得一些结果,但查询失败了!
String sqlFindModel = "SELECT * FROM [PRODUCT] WHERE [PN] LIKE ('*" + textBox1.Text + "*')";
When I trim the "WHERE [PN] LIKE ..." part, it works fine. 当我修剪“WHERE [PN] LIKE ...”部分时,它工作正常。 When I replace LIKE to '=' and look for Exact value, it works.
当我将LIKE替换为'='并查找Exact值时,它可以正常工作。
I am confused. 我很困惑。
PS - It is Interesting, when doing Query in ACCESS directly, you have to use *; PS - 有趣的是,当直接在ACCESS中进行查询时,你必须使用*; but when using C# and connect to MS Access, need to use %... interesting!
但是当使用C#并连接到MS Access时,需要使用%...有趣!
*
isn't used for wildcarding in SQL LIKE statements - %
is. *
不用于SQL LIKE语句中的通配符 - %
是。
However, you shouldn't just change your code to use %
- you should fix your code so it's not vulnerable to SQL injection attacks , instead. 但是,您不应该只是将代码更改为使用
%
- 您应该修复代码,以便它不会受到SQL注入攻击的攻击 。 You should use parameterized SQL instead. 您应该使用参数化SQL。 See the documentation for
OleDbCommand.Parameters
for an example. 有关示例,请参阅
OleDbCommand.Parameters
的文档。
Consider whether Access' undocumented ALike
comparison operator would make this easier to deal with. 考虑Access'未记录的
ALike
比较运算符是否会使这更容易处理。
"SELECT * FROM [PRODUCT] WHERE [PN] ALike '%" + textBox1.Text + "%'"
ALike
signals Access' db engine to expect ANSI wild cards (% and _ instead of * and ?). ALike
引擎需要ANSI通配符(%和_而不是*和?)。 So your query could then work the same regardless of whether you're running it from within an Access session, or from outside an Access session using OleDb. 因此,无论您是在Access会话中运行,还是从使用OleDb的Access会话外部运行,您的查询都可以正常工作。
I've seen objections to ALike
due to the fact that it's not standard SQL. 我已经看到对
ALike
反对意见,因为它不是标准的SQL。 However, when adapting Access queries for other db engines, I much prefer to change ALike
to Like
rather than having to change * and ? 但是,在为其他数据库引擎调整Access查询时,我更喜欢将
ALike
更改为Like
而不必更改*和? to % and _. 到%和_。
尝试用%替换*字符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.