C#实现精确查询和模糊查询

来源:互联网 发布:网络教育文凭多少钱 编辑:程序博客网 时间:2024/04/30 15:19

方法一:

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 姓名='{0}'or 电话='{0}' or 电子邮件='{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("没有你要的东西");

             }

            else if (radioButton2.Checked)

            {

                conn.Open();

                string a = "%" + textBox1.Text + "%";

                string sql = string.Format("SELECT * FROM MANLIAN WHERE 姓名 like '{0}' or 电话 like '{0}' or 电子邮件 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("没有你要的东西!");

            }

            else

            {

                MessageBox.Show("请选择查询方式!");

            }

        }

    }

}



方法二: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();        } 
原创粉丝点击