C#三层架构ComboBox控件三级联动

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




实现效果:

实现效果

数据库:(三个数据表)

省数据表

市区数据表

区县数据表

两层架构(UI和Model)

Form1.cs代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
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 TwoComboBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void LoadProvinceData()
        {
            string testDB = ConfigurationManager.ConnectionStrings["testDB"].ConnectionString;
            SqlConnection conn = new SqlConnection(testDB);
            string sql = "select AreaId,AreaName from T_Province where AreaPid=0";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    int id = reader.GetInt32(0);
                    string name = reader.GetString(1);
                    Area area = new Area(id, name);
                    cboProvince.Items.Add(area);
                }
            }
        }
        private void LoadCity(int id)
        {
            string testDB = ConfigurationManager.ConnectionStrings["testDB"].ConnectionString;
            SqlConnection conn = new SqlConnection(testDB);
            string sql = "select CityId,CityName from T_City where AreaId=@id";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@id",id);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    int aid = reader.GetInt32(0);
                    string aname = reader.GetString(1);
                    Area area = new Area(aid,aname);
                    cboCity.Items.Add(area);
                }
            }
        }
        private void LoadCounty(int id)
        {
            string testDB = ConfigurationManager.ConnectionStrings["testDB"].ConnectionString;
            SqlConnection conn = new SqlConnection(testDB);
            string sql = "select CountyId,CountyName from T_Area where CityId=@id";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@id", id);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    int cid = reader.GetInt32(0);
                    string name = reader.GetString(1);
                    Area area = new Area(cid, name);
                    comboBox1.Items.Add(area);
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadProvinceData();
            cboProvince.SelectedIndex = 0;
            cboCity.SelectedIndex = 0;
        }
        private void cboProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboProvince.SelectedItem!=null)
            {
                Area area = cboProvince.SelectedItem as Area;
                if (area!=null)
                {
                    int id = area.AreaId;
                    cboCity.Items.Clear();
                    LoadCity(id);
                    cboCity.SelectedIndex = 0;
                }
            }
        }
        private void cboCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboCity.SelectedItem != null)
            {
                Area area = cboCity.SelectedItem as Area;
                if (area != null)
                {
                    int id = area.AreaId;
                    comboBox1.Items.Clear();
                    LoadCounty(id);
                    comboBox1.SelectedIndex = 0;
                }
            }
        }
    }
}

Area.cs类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TwoComboBox
{
    public class Area
    {
        public Area(int id,string name)
        {
            this.AreaId = id;
            this.AreaName = name;
        }
        public int AreaId { get; set; }
        public string AreaName { get; set; }
        public override string ToString()
        {
            return AreaName;
        }
    }
}

三层架构:

项目的数据模块图:

项目结构图

BLL层:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsFormsApp1.DAL;
namespace WindowsFormsApp1.BLL
{
    class AreaBLL
    {
        AreaDAL areaDAL = new AreaDAL();
        public ArrayList LoadProvinceData()
        {
            return areaDAL.LoadProvinceData();
        }
        public ArrayList LoadCityData(int id)
        {
            return areaDAL.LoadCityData(id);
        }
        public ArrayList LoadCountyData(int id)
        {
            return areaDAL.LoadCountyData(id);
        }
    }
}

DAL层:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsFormsApp1.Model;
namespace WindowsFormsApp1.DAL
{
    class AreaDAL
    {
        SQLHelper sqlHelp = new SQLHelper();
        public ArrayList LoadProvinceData()
        {
            ArrayList list = new ArrayList();
            SqlDataReader dr = sqlHelp.ExecuteSqlReader("select AreaId,AreaName from T_Province where AreaPid=0");
            while (dr.Read())
            {
                int id = dr.GetInt32(0);
                string name = dr.GetString(1);
                Area area = new Area(id, name);
                list.Add(area);
            }
            dr.Close();
            return list;
        }
        public ArrayList LoadCityData(int id)
        {
            ArrayList list = new ArrayList();
            SqlDataReader dr = sqlHelp.ExecuteSqlReader("select CityId,CityName from T_City where AreaId=@id",
                new SqlParameter("@id", id));
            while (dr.Read())
            {
                id = dr.GetInt32(0);
                string name = dr.GetString(1);
                Area area = new Area(id, name);
                list.Add(area);
            }
            dr.Close();
            return list;
        }
        public ArrayList LoadCountyData(int id)
        {
            ArrayList list = new ArrayList();
            SqlDataReader dr = sqlHelp.ExecuteSqlReader("select CountyId,CountyName from T_Area where CityId=@id",
                new SqlParameter("@id", id));
            while (dr.Read())
            {
                id = dr.GetInt32(0);
                string name = dr.GetString(1);
                Area area = new Area(id, name);
                list.Add(area);
            }
            dr.Close();
            return list;
        }
    }
}

Model层:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApp1.Model
{
    class Area
    {
        public Area(int id, string name)
        {
            this.AreaId = id;
            this.AreaName = name;
        }
        public int AreaId { get; set; }
        public string AreaName { get; set; }
        public override string ToString()
        {
            return AreaName;
        }
    }
}

UI层:

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 WindowsFormsApp1.BLL;
using WindowsFormsApp1.Model;
namespace WindowsFormsApp1.UI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        AreaBLL areaBLL = new AreaBLL();
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = areaBLL.LoadProvinceData();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Area area = comboBox1.SelectedItem as Area;
            int id = area.AreaId;
            comboBox2.DataSource = areaBLL.LoadCityData(id);
        }
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Area area = comboBox2.SelectedItem as Area;
            int id = area.AreaId;
            comboBox3.DataSource = areaBLL.LoadCountyData(id);
        }
    }
}

还有数据库访问类(SQLHelper.cs)代码太多就不贴出来了

GitHub源码下载:https://github.com/kiritobin/ThreeComboBoxArea

原文链接:https://www.idaobin.com/archives/1093.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

电子邮件地址不会被公开。 必填项已用*标注

58 + = 67