省市查询

app.config

<configuration>
 
    <connectionStrings>
      <add name="conn" connectionString="Data Source=./SQLEXPRESS;
AttachDBFilename=D:/My Documents/Documents/Visual Studio 2010/Projects/省市查询/省市查询/省市.mdf;
Integrated Security=True;User Instance=True" />
     
    </connectionStrings>
 
</configuration>

form1.cs

Code:
  1. using System.Text;   
  2. using System.Windows.Forms;   
  3. using System.Data.SqlClient;   
  4. using System.Configuration;   
  5.   
  6. namespace 省市查询   
  7. {   
  8.     public partial class Form1 : Form   
  9.     {   
  10.         public Form1()   
  11.         {   
  12.             InitializeComponent();   
  13.         }   
  14.   
  15.         private void Form1_Load(object sender, EventArgs e)   
  16.         {   
  17.             string con = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;   
  18.             using (SqlConnection cnn = new SqlConnection(con))   
  19.             {   
  20.                 cnn.Open();   
  21.                 using (SqlCommand cmd = cnn.CreateCommand())   
  22.                 {   
  23.                     cmd.CommandText = "select * from promary";   
  24.                     using (SqlDataReader reader = cmd.ExecuteReader())   
  25.                     {   
  26.                         while(reader.Read())   
  27.                         {   
  28.                             PromaryItem items = new PromaryItem();   
  29.                             items.Id = reader.GetInt32(reader.GetOrdinal("proID"));   
  30.                             items.Name = reader.GetString(reader.GetOrdinal("proName"));   
  31.                             cmd省.Items.Add(items);   
  32.                         }   
  33.                     }   
  34.                        
  35.   
  36.                 }   
  37.             }   
  38.         }   
  39.   
  40.         private void cmd省_SelectedIndexChanged(object sender, EventArgs e)   
  41.         {   
  42.             cmd市.Items.Clear();   
  43.             PromaryItem items = (PromaryItem)cmd省.SelectedItem;   
  44.             int proID = items.Id;   
  45.             string con = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;   
  46.             using (SqlConnection cnn = new SqlConnection(con))   
  47.             {   
  48.                 cnn.Open();   
  49.                 using (SqlCommand cmd = cnn.CreateCommand())   
  50.                 {   
  51.                     cmd.CommandText = "select * from city where proID=@proID";   
  52.                     cmd.Parameters.AddWithValue("proID",proID);   
  53.                     using (SqlDataReader reader = cmd.ExecuteReader())   
  54.                     {   
  55.                         while(reader.Read())   
  56.                         {   
  57.                             string cityName = reader.GetString(reader.GetOrdinal("cityName"));   
  58.                             cmd市.Items.Add(cityName);   
  59.                         }   
  60.                     }   
  61.   
  62.   
  63.                 }   
  64.             }   
  65.         }   
  66.     }   
  67.     class PromaryItem   
  68.     {   
  69.         public string Name{get;set;}   
  70.         public int Id{getset; }   
  71.         
  72.     }   
  73. }  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值