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

方法一:

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

        } 
  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值