String strfield = "id | classname | classadd ";
String strkeywords = This. tbxkeywords. Text. Trim ();
String strsql = dbexe. searchtext ("select * from Class", strfield, strkeywords );
Fuzzy queries with multiple fields are often used. The above functions can be implemented. For example, when the strkeywords value is "script home", you can output:
Select * from class where Id like '% %' or classname like '% %' or classadd like '% %'
Function:
/// <Summary>
/// Fuzzy search of multiple fields based on keywords
/// </Summary>
/// <Param name = "sqlstr"> select * From talbe SQL statement </param>
/// <Param name = "sqltext"> determines the statement condition. It is a string separated by |. </param>
/// <Param name = "keywords"> keyword </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;
}