dropdownlist 连接数据库

protected void Page_Load(object sender, EventArgs e) 
  { 
  if (!IsPostBack) 
  { 
  SqlConnection con = new SqlConnection("server=;database=province;uid=sa;pwd=sa;"); 
  con.Open(); 
  SqlCommand cmd = new SqlCommand("select * from province", con); 
  SqlDataReader sdr = cmd.ExecuteReader(); 
  DropDownList1.DataSource = sdr; 
  DropDownList1.DataTextField = "proname"; 
  DropDownList1.DataValueField = "proid"; 
  DropDownList1.DataBind(); 
  con.Close(); 
  sdr.Close(); 
  } 
   
   
  } 
  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) //选择第一个DropDownList中的项后后数据绑定
  { 
   
  SqlConnection con = new SqlConnection("server=;database=province;uid=sa;pwd=sa;"); 
  con.Open(); 
  SqlCommand cmd2 = new SqlCommand("select * from city where proid=" + DropDownList1.SelectedValue, con); 
  SqlDataReader sdr2 = cmd2.ExecuteReader(); 
  DropDownList2.DataSource = sdr2; 
  DropDownList2.DataTextField = "cityname"; 
  DropDownList2.DataValueField = "cityid"; 
  DropDownList2.DataBind(); 
  DropDownList2.Visible = true; 
  sdr2.Close(); 
  con.Close(); 
   
  } 

 

再例子

protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  DataSet ds = onkhriss.Labor.findBuMen();
  this.DropDownList1.DataSource = ds.Tables[0];
  this.DropDownList1.DataTextField = "d_name";
  this.DropDownList1.DataValueField = "d_id";
  this.DropDownList1.DataBind();
  if (DropDownList2.Text == "")
  {
  int d_id = Convert.ToInt32(this.DropDownList1.SelectedValue);
  DataSet ds2 = onkhriss.Labor.findUser(d_id);
  this.DropDownList2.DataSource = ds2.Tables[0];
  this.DropDownList2.DataTextField = "a_username";
  this.DropDownList2.DataValueField = "a_did";
  this.DropDownList2.DataBind();  
  }
  }
   
  }
  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
  int d_id = Convert.ToInt32(this.DropDownList1.SelectedValue);
  DataSet ds2 = onkhriss.Labor.findUser(d_id);
  this.DropDownList2.DataSource = ds2.Tables[0];
  this.DropDownList2.DataTextField = "a_username";
  this.DropDownList2.DataValueField = "a_did";
  this.DropDownList2.DataBind();  
  }
//查询部门 
  public static DataSet findBuMen() 
  { 
  return (DataSet)db.ExecuteDataSet(CommandType.Text, "select * from tb_department"); 
  } 
//查询用户 
  public static DataSet findUser(int id) 
  { 
  return (DataSet)db.ExecuteDataSet(CommandType.Text, "select * from tb_AdminUser where a_did=" + id); 
  }

 

http://topic.csdn.net/u/20090615/22/40716d78-78c7-4d65-a9fa-78925f3c5e80.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要为dropdownlist绑定数据库中的数据,可以按照以下步骤操作: 1. 在页面中添加一个dropdownlist控件。 2. 在页面的代码-behind中编写代码来连接数据库并查询需要绑定的数据。比如使用ADO.NET中的SqlConnection和SqlCommand类来连接数据库并执行查询操作。 3. 将查询结果绑定到dropdownlist控件上。可以使用dropdownlist控件的DataSource属性来指定数据源,使用DataTextField和DataValueField属性来指定要显示的文本和值的字段名,最后调用DataBind方法来完成绑定。 以下是一个简单的示例代码: ```c# protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string connectionString = "Data Source=your_server;Initial Catalog=your_database;User ID=your_username;Password=your_password"; string query = "SELECT id, name FROM your_table"; SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(query, connection); SqlDataAdapter adapter = new SqlDataAdapter(command); DataSet dataSet = new DataSet(); adapter.Fill(dataSet, "your_table"); dropdownlist.DataSource = dataSet.Tables["your_table"]; dropdownlist.DataTextField = "name"; dropdownlist.DataValueField = "id"; dropdownlist.DataBind(); } } ``` 在这个示例中,我们从名为your_table的表中查询了id和name两个字段的数据,并将其绑定到了名为dropdownlistdropdownlist控件上。其中,数据库连接字符串、查询语句、数据源字段名和值字段名需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值