Membership.FindUsersByName方法模糊查询

MSDN的帮助是不错,可惜举的例子比较特殊,没有解释如何做模糊查询。

只有这么一句:

SqlMembershipProvider 通过对 usernameToMatch 参数使用 LIKE 子句来执行搜索。LIKE 子句中受 SQL Server 支持的任何通配符都可以在 usernameToMatch 参数值中使用。

查了下LIKE子句,英文的在这里

http://msdn.microsoft.com/en-us/library/aa933232(SQL.80).aspx

Wildcard characterDescriptionExample
% Any string of zero or more characters. WHERE title LIKE '%computer%' finds all book titles with the word 'computer' anywhere in the book title.
_ (underscore) Any single character. WHERE au_fname LIKE '_ean' finds all four-letter first names that end with ean (Dean, Sean, and so on).
[ ] Any single character within the specified range ([a-f]) or set ([abcdef]). WHERE au_lname LIKE '[C-P]arsen' finds author last names ending with arsen and beginning with any single character between C and P, for example Carsen, Larsen, Karsen, and so on.
[^] Any single character not within the specified range ([^a-f]) or set ([^abcdef]). WHERE au_lname LIKE 'de[^l]%' all author last names beginning with de and where the following letter is not l.

简单的说,%就可以做模糊查询了,和sql一样,如果前后字符未知,可以这么做 "%" + name + "%",这样就可以实现模糊查询了。当然,更复杂的查询也可以通过不同的通配符组合来实现。

原文地址:https://www.cnblogs.com/kofkyo/p/2297296.html