简体   繁体   English

Access 2010问题与C#中的where子句

[英]Ms Access 2010 issue with a where clause in C#

I have a complex select statement for an Access 2010 database which grabs data from multiple tables using several LEFT JOIN statements. 我有一个Access 2010数据库的复杂select语句,它使用几个LEFT JOIN语句从多个表中获取数据。 The query works as expected and I get the entire table. 查询按预期工作,我得到整个表。

So now I want to add search functionality. 所以现在我想添加搜索功能。 One way was to add a WHERE clause at the end of the query and reference one of the JOIN ed tables' text field and compare it against some text ( WHERE [All Names].Name LIKE "*Mark*" ). 一种方法是在查询结尾添加一个WHERE子句,并引用其中一个JOIN ed表的文本字段,并将其与某些文本进行比较( WHERE [All Names].Name LIKE "*Mark*" )。

Second option I tried was select * from (**complex sql here**) where **condition** 我试过的第二个选项是select * from (**complex sql here**) where **condition**

Now in both cases, when my condition is something simple like ([ID]<15) , it works like a charm, but when I change it to ([Employee Name] LIKE "\\*Mark\\*") or the one in option 1, it produces an empty data table as if the request goes through, there is no error or exception, all the field names are present, but no rows are returned. 现在在这两种情况下,当我的条件很简单时([ID]<15) ,它就像一个魅力,但是当我把它改为([Employee Name] LIKE "\\*Mark\\*")或其中一个选项1,它产生一个空数据表,好像请求通过,没有错误或异常,所有字段名称都存在,但没有返回任何行。

However, if I grab the full string of the generated SQL string (either option) using the debugger (or just dump it into a text file), and then with literally no changes put that string directly into a new Access query, it works fine and returns several fields where the name contains "Mark" 但是,如果我使用调试器获取生成的SQL字符串的完整字符串(任一选项)(或者只是将其转储到文本文件中),然后实际上没有任何更改将该字符串直接放入新的Access查询中,它工作正常并返回名称中包含“Mark”的多个字段

Very simply put, a query that works fine within Access, does not work from within C#. 非常简单地说,在Access中工作正常的查询在C#中不起作用。

So I am now confused 所以我现在很困惑

You're using OleDb to connect to the Access db file. 您正在使用OleDb连接到Access数据库文件。 In that situation you must use ANSI wild cards ( % and _ ) for a Like comparison instead of * and ? 在这种情况下,您必须使用ANSI通配符( %_ )进行Like比较而不是*? .

Use a pattern like this in your WHERE clause. WHERE子句中使用这样的模式。

WHERE [Employee Name] LIKE "%Mark%"

If you want a query which works the same within an Access session as it does from an OleDb connection, use ALIKE instead of LIKE . 如果您希望在Access会话中使用与OleDb连接相同的查询,请使用ALIKE而不是LIKE ALIKE always uses the ANSI wild cards. ALIKE始终使用ANSI通配符。

WHERE [Employee Name] ALIKE "%Mark%"

Simon's question and HansUp's answer ended up solving my problem. Simon的问题和HansUp的答案最终解决了我的问题。 For those curious or having a similar problem, here is full query: 对于那些好奇或有类似问题的人,这里是完整的查询:

string query=
  "SELECT Employees.ID, " +
  "[All Names E].Name AS [Employee Name], " +
  "Titles.Title, " +
  "[All Names S].Name AS [Supervisor Name], " +
  "Employees.[Phone #], " +
  "Offices.[Office Location], " +
  "PCs.PC " +
  "FROM (((((Employees LEFT JOIN [All Names] as [All Names E] ON Employees.Employee = [All Names E].ID) " +
  "LEFT JOIN [All Names] as [All Names S] on Employees.Supervisor=[All Names S].ID) " +
  "LEFT JOIN Titles on Employees.Title=Titles.ID) " +
  "LEFT JOIN Offices on Employees.[Office Location]=Offices.ID) " +
  "LEFT JOIN PCs on Employees.PC=PCs.ID) " +
  "ORDER BY Employees.ID";

Adding a where clause before ORDER BY that is WHERE ([All Names E].Name LIKE \\"*Mark*\\") did work from within Access. ORDER BY之前添加where子句是WHERE ([All Names E].Name LIKE \\"*Mark*\\")在Access中工作。 The second way was: 第二种方式是:

string searchQuery="select * from ("+query+") where ([Employee Name] like \"*Mark*\")";

Both methods worked perfectly in Access, but I had no idea there was a different wildcard symbol for use with OleDB. 两种方法在Access中都运行良好,但我不知道有一个不同的通配符符号可用于OleDB。

So changing the asterisk to a percentage sign fixed the issue. 因此,将星号更改为百分号可解决问题。

Thanks again. 再次感谢。

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

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