Chinaunix首页 | 论坛 | 博客
  • 博客访问: 526985
  • 博文数量: 88
  • 博客积分: 2256
  • 博客等级: 大尉
  • 技术积分: 921
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-08 23:20
个人简介

积硅步,行千里

文章分类

全部博文(88)

文章存档

2019年(5)

2018年(1)

2016年(15)

2015年(23)

2013年(3)

2012年(6)

2011年(3)

2010年(22)

2009年(10)

我的朋友

分类: C#/.net

2015-04-16 09:08:55

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10. namespace 省市
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();}
  17.  private void Form1_Load(object sender, EventArgs e)
  18.         {
  19.             using (SqlConnection conn = new SqlConnection("server=.;database=DBPromary;user id=sa;password=123456"))
  20.             {
  21.                 conn.Open(); using (SqlCommand cmd = conn.CreateCommand())
  22.                 {
  23.                     cmd.CommandText = "select proName from promary"; using (SqlDataReader reader = cmd.ExecuteReader())
  24.                     { while (reader.Read())
  25.                         {
  26.                             string name = reader.GetString(reader.GetOrdinal("proName")); cb1.Items.Add(name);
  27.                         }
  28.                     }
  29.                 }
  30.             }
  31. }
  32.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  33.         {
  34.             cb2.Items.Clear();
  35.             using (SqlConnection conn = new SqlConnection("server=.;database=DBPromary;user id=sa;password=123456"))
  36.             {
  37.                 conn.Open();
  38.                 using (SqlCommand cmd = conn.CreateCommand())
  39.                 {
  40.                     cmd.CommandText = "select cityName from city,promary where city.proID=promary.proID and promary.proName=@pname";
  41.                     cmd.Parameters.Add(new SqlParameter("pname",cb1.SelectedItem));
  42.                     using (SqlDataReader reader = cmd.ExecuteReader())
  43.                     {
  44.                         while (reader.Read())
  45.                         {
  46.                             string name = reader.GetString(reader.GetOrdinal("cityName"));
  47.                             cb2.Items.Add(name);
  48.                         }
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.     }
  54. }

阅读(2578) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~