asp.net multi-field Fuzzy query code _ Practical skills
Source: Internet
Author: User
String Strfield = "Id|classname|classadd";
String strkeywords = This.tbxKeyWords.Text.Trim ();
String strSQL = Dbexe.searchtext ("SELECT * from Class", Strfield, Strkeywords);
Multiple-field fuzzy queries are often used, and the above functions can be implemented, for example, when the Strkeywords value is "cloud-dwelling community", you can output:
SELECT * from class where ID like '% cloud habitat Community% ' or className like '% cloud habitat Community% ' or classadd like '% cloud habitat Community '
Function:
<summary>
Implement multi-field fuzzy query based on key words
</summary>
<param name= "Sqlstr" >select * from Talbe SQL statements </param>
<param name= "SQLText" > Judgment Statement Condition, is a separate string </param>
<param name= "keywords" > keywords </param>
public static string SearchText (String strSQL, string Strfield, String keywords)
{
StringBuilder sb = new StringBuilder (strSQL);
if (Strfield!= string. Empty)
{
Sb. Append ("where");
string[] Arrkey = strfield.split (' | ');
for (int i = 0; i < arrkey.length; i++)
{
Sb. Append (Arrkey[i] + "like '%" + keywords + "% ' or");
}
String str = sb. ToString ();
Remove the last "or"
if (str. IndexOf ("or") >= 0)
{
Return str. Remove (str. LastIndexOf ("or"));
}
return str;
}
return strSQL;
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service