简体   繁体   English

查询不返回任何内容

[英]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.

相关问题 使用DirectorySearcher的C#中的LDAP查询不返回任何内容 - LDAP Query in c# using DirectorySearcher does not return anything 为什么GetFields()不返回任何内容? - Why does GetFields() not return anything? XSLT转换不会返回任何内容 - XSLT transformation does not return anything 查询在SQL Server Management Studio中返回结果,但在应用程序中不返回任何内容 - Query returns result in SQL Server Management Studio, but does not return anything in an application unity Firebase RealtimeDatabase GetValueAsync() 不返回任何内容 - Unity Firebase RealtimeDatabase GetValueAsync() does not return anything WNetOpenEnum 在根级别之后不返回任何内容 - WNetOpenEnum does not return anything after root level 如果查询未返回任何内容,LINQ选择项的默认值 - Default value for linq select item if query didn't return anything Linq查询不会返回任何内容,即使DateTime值相同 - Linq query doesn't return anything even if DateTime values are the same 通过字符串查询ToolStripMenuItem.DropDownList不会返回任何内容 - ToolStripMenuItem.DropDownList query via string won't return anything 除了返回一行,DataTable.NewRow还能做什么吗? - Does DataTable.NewRow do anything other than return a row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM