您的位置:首页 > 博客中心 > 数据库 >

ComboBox连接数据库、显示下拉框选项

时间:2022-03-14 06:07

     ?本文从http://blog.csdn.net/susidian/article/details/7027007处学习得来。    

    ?如何将combobox控件绑定数据库,并在下拉框中显示从数据库中查找得到的数据。如何在上一个combobox框中筛选下一个combobox中可以选择的的选项。   技术分享       贴出代码:  
using System;
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 Test
{
    public class GetData
    {
        public string province { get; set; }

        public override string ToString()
        {
            return province;
        }
    }
    
    public partial class Form1 : Form
    {
        ComboBox combobox_P;
        ComboBox combobox_C;
        SqlCommand command;
        SqlConnection connection;
        string ConnectionString = "Data Source = 1111; Initial Catalog = Test; User Id = sa; Password = ****";


        public Form1()
        {
            InitializeComponent();
            Surface();
        }

        //创建好combobox框
        public void Surface()
        {
            combobox_P = new ComboBox();
            combobox_P.Size = new Size(121, 20);
            combobox_P.Location = new Point(186, 52);
            this.Controls.Add(combobox_P);
            combobox_P.SelectedIndexChanged += new EventHandler(combobox_P_SelectedIndexChanged);//选择事件
            

            combobox_C = new ComboBox();
            combobox_C.Size = new Size(121, 20);
            combobox_C.Location = new Point(313, 52);
            this.Controls.Add(combobox_C);
        
        }

        private void combobox_P_SelectedIndexChanged(object sender, EventArgs e)
        {
            combobox_C.Items.Clear();  //清除之前的下拉选项
            GetData getdata = (GetData)combobox_P.SelectedItem;   //获取当前选好的选项
            string province = getdata.province;   //将选好的选项中的字符串获取
            using (connection = new SqlConnection(ConnectionString))   //连接数据库
            {
                connection.Open();

                using (command = connection.CreateCommand())
                {
                    SqlDataReader reader;   //数据读取类
                    command.Parameters.Add(new SqlParameter("province", province));   //更新province
                    command.CommandText = "select city from City where province = @province;";
                    
                    using (reader = command.ExecuteReader())  //返回一个SqlDataReader
                    {
                        while (reader.Read())  //一个个数据读取
                        {
                            string cityName = reader.GetString(reader.GetOrdinal("city"));   //获取序列号
                            combobox_C.Items.Add(cityName);
                        }
                    }
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //打开数据库      
            using(connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                using (command = new SqlCommand("select * from Province;", connection))  //查询操作
                {
                    SqlDataReader reader = command.ExecuteReader();  //返回一个SqlDataReader
                    while (reader.Read())   //一个个读数据
                    {
                        GetData getdata = new GetData();
                        getdata.province = reader.GetString(reader.GetOrdinal("province"));   //获取返回读的字符串

                        combobox_P.Items.Add(getdata);   //添加到下拉框选项
                    }
                }
            }
        }

    }
}

 

本类排行

今日推荐

热门手游