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 (); }