ADO.NET省市选择代码

本文介绍了一个基于C#的Windows Forms应用程序,该程序实现了一个省市选择器功能。通过连接到本地数据库,程序能够加载省份列表,并在用户选择省份后加载对应的城市列表。此外,还展示了如何在选定城市后获取区号等附加信息。



代码

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 省市选择
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            using (SqlConnection coon = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\新建文件夹\exam\test9\SQLtest9.mdf;Integrated Security=True"))
            {
                cmbshi.Items.Clear();
                coon.Open();
                using(SqlCommand cmd=coon.CreateCommand())
                {
                    cmd.CommandText = "select * from shen;";
                    using(SqlDataReader reader=cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            ids id = new ids();
                            id.id= reader.GetInt32(reader.GetOrdinal("id"));
                            id.shen=reader.GetString(reader.GetOrdinal("省"));
                            cmbshen.Items.Add(id);
                        }

                    }
                }
            }
        }

        private void cmbshen_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtqh.Clear();
            ids item = (ids)cmbshen.SelectedItem;
            int id1 = item.id;
            using (SqlConnection coon = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\新建文件夹\exam\test9\SQLtest9.mdf;Integrated Security=True"))
            {
                coon.Open();
                using (SqlCommand cmd = coon.CreateCommand())
                {
                    cmd.CommandText = "select * from city where id=@id";
                    cmd.Parameters.Add(new SqlParameter("@id", id1));
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            ids shi=new ids();
                            cmbshi.Items.Add(reader.GetString(reader.GetOrdinal("市")));
                            
                        }

                    }
                }

            }
           
        }

        private void cmbshi_SelectedIndexChanged(object sender, EventArgs e)
        {
            string shi = (string)cmbshi.SelectedItem;
            using (SqlConnection coon = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\新建文件夹\exam\test9\SQLtest9.mdf;Integrated Security=True"))
            {
                coon.Open();
                using (SqlCommand cmd = coon.CreateCommand())
                {
                    cmd.CommandText = "select * from city where 市=@id";
                    cmd.Parameters.Add(new SqlParameter("@id", shi));
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            
                           txtqh.Text=reader.GetString(reader.GetOrdinal("quhao"));

                        }

                    }
                }

            }
        }
    }
    class ids
    {
       public int id{get;set;}
       public string shen { get; set; }
    }
}



效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值