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

您所在的位置:网站首页 行业分类查询精准查询 C#实现精确查询和模糊查询

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

2024-06-20 06:15| 来源: 网络整理| 查看: 265

方法一:

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


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3