vs c#通过ComboBox选择查询类别并在TextBox输入条件进行查询

时间:2024-04-12 09:51:21


vs2015 c#通过ComboBox选择查询类别并在TextBox输入条件进行查询vs2015 c#通过ComboBox选择查询类别并在TextBox输入条件进行查询tvs2015 c#通过ComboBox选择查询类别并在TextBox输入条件进行查询

using Sytem;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 通过ComboBox选择查询类别并在TextBox输入条件进行查询
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("server=WINDOWS-CBBU2E6;database=OrderManagementSystem;Uid = sa; Pwd = sa123");       //连接数据库
            cn.Open();                                                                            //打开连接的数据库
            SqlDataAdapter dap = new SqlDataAdapter("SELECT * FROM dbo.Table_2", cn);    //建立SQL语句与数据库的连接
            DataSet ds = new DataSet();                                                                //实例化DataSet类
            dap.Fill(ds, "Table");                                                                     //添加SQL语句并执行
            string[] arylist = new string[ds.Tables[0].Columns.Count];                            //按照列数定义字符串数组
            for (int i = 0; i < ds.Tables[0].Columns.Count; i++)                               //遍历列
            {
                arylist[i] = ds.Tables[0].Columns[i].ColumnName;                                 //获取数据表中的列名
            }
            for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
            {
                comboBox2.Items.Add(arylist[j]);                                                        //将列名添加到comboBox1控件中
            }
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }
        SqlConnection conn;
        private void button8_Click(object sender, EventArgs e)
        {
            if (this.comboBox2.SelectedIndex == 0)//comboBox2下拉框选择笫一项
            {
                conn = new SqlConnection("server=WINDOWS-CBBU2E6;database=OrderManagementSystem;Uid = sa; Pwd = sa123");
                SqlCommand cmd = new SqlCommand("select * from dbo.Table_2 where A like '%" + textBox1.Text + "%'", conn);
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmd;
                DataSet ds = new DataSet();
                sda.Fill(ds, "dbo.Table_2");
                dataGridView1.DataSource = ds.Tables["dbo.Table_2"];
            }

            if (this.comboBox2.SelectedIndex == 1)
            {
                conn = new SqlConnection("server=WINDOWS-CBBU2E6;database=OrderManagementSystem;Uid = sa; Pwd = sa123");
                SqlCommand cmd = new SqlCommand("select * from dbo.Table_2 where B like '%" + textBox1.Text + "%'", conn);
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmd;
                DataSet ds = new DataSet();
                sda.Fill(ds, "dbo.Table_2");
                dataGridView1.DataSource = ds.Tables["dbo.Table_2"];
            }
            if (this.comboBox2.SelectedIndex == 2)
            {
                conn = new SqlConnection("server=WINDOWS-CBBU2E6;database=OrderManagementSystem;Uid = sa; Pwd =sa123");
                SqlCommand cmd = new SqlCommand("select * from dbo.Table_2 where C like '%" + textBox1.Text + "%'", conn);
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmd;
                DataSet ds = new DataSet();
                sda.Fill(ds, "dbo.Table_2");
                dataGridView1.DataSource = ds.Tables["dbo.Table_2"];
            }
            if (this.comboBox2.SelectedIndex == 3)
            {
                conn = new SqlConnection("server=WINDOWS-CBBU2E6;database=OrderManagementSystem;Uid = sa; Pwd = sa123");
                SqlCommand cmd = new SqlCommand("select * from dbo.Table_2 where D like '%" + textBox1.Text + "%'", conn);
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmd;
                DataSet ds = new DataSet();
                sda.Fill(ds, "dbo.Table_2");
                dataGridView1.DataSource = ds.Tables["dbo.Table_2"];
            }

        }
    }
}