Asp.net keyword fuzzy query (similar to the search function of major websites)

Source: Internet
Author: User

I would like to share with you todayAlgorithmI wrote it in C #. Other languages just need to change the syntax. Fuzzy search with a space disconnection keyword is similar to the search function of major websites. What I want is a process of splicing SQL statements. The Delimiter is a space disconnection keyword. In either case, the Delimiter is a space disconnection relationship that is "and", and the Delimiter is a space disconnection relationship that is "or. In fact, it is also very simple.CodeRight.

Link code of the keyword "or:

 ///   <Summary>      ///  Relationship of keyword "or"  ///   </Summary>      ///   <Returns> </returns>      Public   String Orkeywords (){  String SQL = "  Select username from usertable where 1 = 1  "  ;  String Txtkeyword = Txttitle. Text. Trim ();  If (Txtkeyword = ""  ) {Clientscript. registerstartupscript (  Typeof (Page ), " AA  " , "  Alert ('enter a keyword! ')  " , True  );  Return  ;}  Else  {  Int Len = Txtkeyword. length;  Int N = 0 ;  String Strsql = "  And (  "  ;  String Strkey = ""  ;  For ( Int I = 0 ; I <Len; I ++ ){  If (Txtkeyword [I]. tostring () ="   "  ){  If (I> 0  ){  If (Txtkeyword [I- 1 ]. Tostring ()! = "   "  ) {N ++ ; Strsql + = " Charindex ('  " + Strkey + "  ', Username)> 0 or  "  ; Strkey = ""  ;}}}  Else  {Strkey + = Txtkeyword [I]. tostring ();}}  If (N =0  ) {SQL + = "  And charindex ('  " + Txtkeyword + "  ', Username)> 0 order by ID DESC  "  ;}  Else  {Strsql + = "  Charindex ('  " + Strkey +"  ', Username)> 0)  "  ; SQL + = "" + Strsql + "  Order by ID DESC  "  ;}  //  Splicing completed              Return  SQL ;}} 

Link code with the keyword "and:

 ///  <Summary>      ///  Relationship of the keyword "and"  ///   </Summary>      ///   <Returns> </returns>      Public   String  Andkeywords (){  String SQL = "  Select username from usertable where 1 = 1  "  ; String Txtkeyword = Txttitle. Text. Trim ();  If (Ttxtkeyword = ""  ) {Clientscript. registerstartupscript (  Typeof (Page ), "  AA  " , "  Alert ('enter a keyword! ')  " , True  ); Return  ;}  Else  {  Int Len = Txtkeyword. length;  Int N = 0  ;  String Strsql = "  And  "  ;  String Strkey =""  ;  For ( Int I = 0 ; I <Len; I ++ ){  If (Txtkeyword [I]. tostring () = "   "  ){  If (I> 0  ){  If (Txtkeyword [I- 1 ]. Tostring ()! = "   "  ) {N ++ ; Strsql + = "  Charindex ('  " + Strkey + "  ', Username)> 0 and  "  ; Strkey = "" ;}}}  Else  {Strkey + = Txtkeyword [I]. tostring ();}}  If (N = 0  ) {SQL + = "  And charindex ('  " + Txtkeyword + "  ', Username)> 0 order by ID DESC "  ;}  Else  {Strsql + = "  Charindex ('  " + Strkey + "  ', Username)> 0  "  ; SQL + = "" + Strsql + "  Order by ID DESC  " ;}  //  Splicing completed              Return  SQL ;}} 

The code is very simple. I will not explain it. I will record it myself and hope it will help you.

PS: Tandy tang I wish you a pleasant time writing your code!

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.