C#中ComBox控件配合数据库实现简单的省市选择

首先,数据库中所有省市数据存放在China数据库中的Area表中,简单数据自己随便添加!没什么可说的!

比较简陋的运行效果

C#中ComBox控件配合数据库实现简单的省市选择_第1张图片
区域选择.png

SQL语句

/****** Script for SelectTopNRows command from SSMS  ******/
USE China;
CREATE TABLE Area
(
AreaId INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
AreaName NVARCHAR(32) NULL,
AreaPid INT NOT NULL
)

数据库数据格式就是这样而已

C#中ComBox控件配合数据库实现简单的省市选择_第2张图片
数据.png

其次,应用程序配置文件中添加数据库连接字符串



  
    
  
     
        
    

数据对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProvinceCitySelected
{
    class AreaInfo
    {
        public int AreaId { get; set; }
        public int AreaPid { get; set; }
        public string AreaName { get; set; }
        public override string ToString()
        {
            return this.AreaName;
        }
    }
}

导出数据

        private void buttonExport_Click(object sender, EventArgs e)
        {
            #region 保存文件
            string fileName = string.Empty;//保存的文件名
            //让用户选择要保存文件的路径
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                if (sfd.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                fileName = sfd.FileName;
            } 
            #endregion

            string strConnect = ConfigurationManager.ConnectionStrings["strConnect"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnect))
            {
                con.Open();
                using (SqlCommand cmd  = con.CreateCommand())
                {
                    cmd.CommandText = "select AreaId,AreaName,AreaPid from Area";
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        string tempLine = string.Empty;
                        using (StreamWriter writer = new StreamWriter(fileName))
                        {
                            while (reader.Read())
                            {
                                tempLine = reader["AreaId"] + "," + reader["AreaName"] + "," + reader["AreaPid"];
                                writer.WriteLine(tempLine);//写入文本文件
                            }
                        }
                        
                    }
                }
            }
        }

实现代码也很简单

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

namespace ProvinceCitySelected
{
    public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            string strConnect = ConfigurationManager.ConnectionStrings["strConnect"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnect))
            {
                con.Open();
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = @"select AreaId,AreaName,AreaPid from Area where AreaPid=0";
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        //读取一行数据
                        while (reader.Read())
                        {
                            //把表格数据转换成对象的数据
                            AreaInfo areaInfo = new AreaInfo();
                            areaInfo.AreaId = int.Parse(reader["AreaId"].ToString());
                            areaInfo.AreaPid = int.Parse(reader["AreaPid"].ToString());
                            areaInfo.AreaName = reader["AreaName"].ToString();
                            //将省的信息放到ComBox中。ComBox显示信息是Item对象的ToString()
                            this.comboBoxProvince.Items.Add(areaInfo);
                        } //end while
                    }//end using reader
                }//end using cmd
            }//end using con
            //默认选择第一条数据
            this.comboBoxProvince.SelectedIndex = 0;
        }

        private void comboBoxProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            AreaInfo provinceInfo = this.comboBoxProvince.SelectedItem as AreaInfo;
            //判断是否 拿到的城市为空
            if (provinceInfo == null)
            {
                return;
            }
            //根据省的ID获取所有的城市信息
            
            //加载数据库中的所有省的数据
            string strConnect = ConfigurationManager.ConnectionStrings["strConnect"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnect))
            {
                con.Open();
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = @"select AreaId,AreaName,AreaPid from Area where AreaPid="+provinceInfo.AreaId;
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        this.comboBoxCity.Items.Clear();
                        //读取一行数据
                        while (reader.Read())
                        {
                            //把表格数据转换成对象的数据
                            AreaInfo areaInfo = new AreaInfo();
                            areaInfo.AreaId = int.Parse(reader["AreaId"].ToString());
                            areaInfo.AreaPid = int.Parse(reader["AreaPid"].ToString());
                            areaInfo.AreaName = reader["AreaName"].ToString();
                            //将省的信息放到ComBox中。ComBox显示信息是Item对象的ToString()
                            this.comboBoxCity.Items.Add(areaInfo);
                        } //end while
                    }//end using reader
                }//end using cmd
            }//end using con
            //默认选择第一条数据
            this.comboBoxCity.SelectedIndex = 0;
        }

        //导出数据
        private void buttonExport_Click(object sender, EventArgs e)
        {
            #region 保存文件
            string fileName = string.Empty;//保存的文件名
            //让用户选择要保存文件的路径
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                if (sfd.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                fileName = sfd.FileName;
            } 
            #endregion

            string strConnect = ConfigurationManager.ConnectionStrings["strConnect"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnect))
            {
                con.Open();
                using (SqlCommand cmd  = con.CreateCommand())
                {
                    cmd.CommandText = "select AreaId,AreaName,AreaPid from Area";
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        string tempLine = string.Empty;
                        using (StreamWriter writer = new StreamWriter(fileName))
                        {
                            while (reader.Read())
                            {
                                tempLine = reader["AreaId"] + "," + reader["AreaName"] + "," + reader["AreaPid"];
                                writer.WriteLine(tempLine);//写入文本文件
                            }
                        }
                        
                    }
                }
            }
        }
    }
}

你可能感兴趣的:(C#中ComBox控件配合数据库实现简单的省市选择)