Request returns nothing
I am trying to get some results from the database, but the query is not working!
String sqlFindModel = "SELECT * FROM [PRODUCT] WHERE [PN] LIKE ('*" + textBox1.Text + "*')";
When I trim the "WHERE [PN] LIKE ..." part, it works fine. When I replace LIKE with '=' and search for Exact value, it works.
I am embarrassed.
PS - Interestingly, when you execute Query directly in ACCESS you should use *; but when using C # and connecting to MS Access you need to use% ... interesting!
source to share
*
not used for wildcards in SQL LIKE expressions - %
yes.
However, you shouldn't just change your code to use %
- instead, you should fix your code so that it is not vulnerable to SQL injection attacks . You should use parameterized SQL instead. See the documentation for an example OleDbCommand.Parameters
.
source to share
Consider whether Access' undocumented comparison operator makes it easier to access this comparison operator ALike
.
"SELECT * FROM [PRODUCT] WHERE [PN] ALike '%" + textBox1.Text + "%'"
ALike
signals that ANSI wild cards are expected to access the db engine (% and _ instead of * and?). This way your request can work the same whether you run it from an access session or outside an access session using OleDb.
I have seen objections ALike
to the fact that this is not standard SQL. However, when adapting Access queries to other db models, I prefer to change ALike
to Like
instead of changing * and? up to% and _.
source to share