简体   繁体   中英

Query Access with wildcard

i am trying to do a substring search from an access 2010 database in winforms.

command.Parameters.AddWithValue("@searchTerm", searchTerm);
command.CommandText = "SELECT [OA_Name] FROM [Operating_Authority_Table] WHERE [OA_Name] = [@%searchTerm%]";

I tried to do a full string search and able to do just that, but I can't get any successful searches when i change the term to be a substring.

am i implementing the wildcard character wrongly?

Try something like this:

command.Parameters.AddWithValue("@searchTerm", searchTerm); 
command.CommandText = "SELECT [OA_Name] FROM [Operating_Authority_Table] WHERE [OA_Name] = '%' + @searchTerm + '%'";

The accepted answer:

"...WHERE [OA_Name] = '%' + @searchTerm + '%'";

This will treat the % characters as text literals.

I was slightly surprised because the word "Wildcard" in the question title suggests the intent is for pattern matching. If this is indeed the case, I would recommend replacing the = equality operator with the ALIKE operator eg

"...WHERE [OA_Name] ALIKE '%' + @searchTerm + '%'";

The problem with the LIKE operator is that the Access database engine (ACE, Jet, whatever) uses different wildcard characters depending on so-called ANSI Query Mode . Presumably you are using ANSI-92 Query Mode by implication of using SqlOleDb (but would you have known for sure?)

The advantage of ALIKE is that is uses the same 'Standard' wildcard characters regardless of ANSI Query Mode, being % for multiple characters and _ for a single character.

尝试使用SQL LIKE运算符

 command.CommandText = "SELECT [OA_Name] FROM [Operating_Authority_Table] WHERE [OA_Name] LIKE '%"+ searchTerm +"%'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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