C # Implementation of accurate query and fuzzy query

Source: Internet
Author: User

Method One:

Using System;

Using System.Collections.Generic;

Using System.ComponentModel;

Using System.Data;

Using System.Drawing;

Using System.Text;

Using System.Windows.Forms;

Using System.Data.SqlClient;

Namespace WindowsFormsApplication7

{

public partial class Form1:form

{

private static string strconnect = "Data source=.//sqlexpress;database=test; Uid=sa; Pwd=sa; ";

Private SqlConnection conn = new SqlConnection (StrConnect);

Public Form1 ()

{

InitializeComponent ();

}

private void Button1_Click (object sender, EventArgs e)

{

if (radiobutton1.checked)

{

Conn. Open ();

String sql = string. Format ("select * from Manlian WHERE name = ' {0} ' or phone = ' {0} ' or e-mail = ' {0} '", TextBox1.Text);

SqlCommand Jingzhun = new SqlCommand (SQL, conn);

SqlDataReader reader = Jingzhun. ExecuteReader ();

while (reader. Read ())

{

string[] str = new STRING[3];

Str[0] = reader[0]. ToString ();

STR[1] = reader[1]. ToString ();

STR[2] = reader[2]. ToString ();

ListViewItem li = new ListViewItem ();

Li. Subitems.clear ();

Li. Subitems[0]. Text = str[0];

Li. SubItems.Add (str[1]);

Li. SubItems.Add (str[2]);

Lv. Items.Add (LI);

Reader. Close ();

Conn. Close ();

} MessageBox.Show ("Nothing you Want");

}

else if (radiobutton2.checked)

{

Conn. Open ();

String a = "%" + TextBox1.Text + "%";

String sql = string. Format ("select * from Manlian WHERE name like ' {0} ' or phone like ' {0} ' or e-mail like ' {0} '", a);

SqlCommand Mohu = new SqlCommand (SQL, conn);

SqlDataReader reader = Mohu. ExecuteReader ();

while (reader. Read ())

{

string[] str = new STRING[3];

Str[0] = reader[0]. ToString ();

STR[1] = reader[1]. ToString ();

STR[2] = reader[2]. ToString ();

ListViewItem li = new ListViewItem ();

Li. Subitems.clear ();

Li. Subitems[0]. Text = str[0];

Li. SubItems.Add (str[1]);

Li. SubItems.Add (str[2]);

Lv. Items.Add (LI);

Reader. Close ();

Conn. Close ();

}messagebox.show ("There's nothing you want.") ");

}

Else

{

MessageBox.Show ("Please select the way to query!");

}

}

}

}

Method two: Public string[] Getchaxunstr (String strtemp) {string[] stra= strtemp.
   Split (")";
   String strb= ""; for (int i = 0;i<stra.
   length;i++) {STRB + + stra[i]+ "&"; } strtemp = Strb. Substring (0,STRB.
   LENGTH-1); Stra= strtemp.
   Split (")";
   Strb= ""; for (int i = 0;i<stra.
   length;i++) {STRB + + stra[i]+ "&"; Return STRB. Substring (0,STRB. LENGTH-1).
  Split (' & '); 
            } protected void button1_click (object sender, EventArgs e) {SqlConnection cnn = new SqlConnection ();
            SqlCommand cmd = new SqlCommand (); Cmd.
   Connection = CNN;
   String strSQL = "SELECT * from Temp_messi where 1<>1";
   String strkey = TextBox1.Text;
     string[] key = Getchaxunstr (strkey); for (int i=0;i<key.             
     length;i++) {strSQL + = "or (name like '%" +key[i]+ "%"); 
                        } cmd.commandtext = strSQL;
            SqlDataAdapter ADP = new SqlDataAdapter ();Adp.
            SelectCommand = cmd;
            DataSet ds = new DataSet (); Adp.
 Fill (DS); Gridview1.datasource = ds.
            Tables[0];

        Gridview1.databind ();  }

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.