1. 绑定数据库的方法

ListBox绑定数据库两种方法 dropdownlist SqlConnection con=new SqlConnection("server=(local);database=department;uid=sa;pwd=");con.Open();
ListBox绑定数据库两种方法 dropdownlistSqlCommand cm
=new SqlCommand("select empID,empName from emp",con);
ListBox绑定数据库两种方法 dropdownlistSqlDataReader dr
=cm.ExecuteReader();//绑定
ListBox绑定数据库两种方法 dropdownlist
this.lbxEmp.DataSource=dr;        //lbxEmp为ListBox对象
ListBox绑定数据库两种方法 dropdownlist
this.lbxEmp.DataTextField="empName";
ListBox绑定数据库两种方法 dropdownlist
this.lbxEmp.DataValueField="empID";
ListBox绑定数据库两种方法 dropdownlist
this.lbxEmp.DataBind();
ListBox绑定数据库两种方法 dropdownlistdr.Close();con.
ListBox绑定数据库两种方法 dropdownlistClose();

2. 使用集合添加

ListBox绑定数据库两种方法 dropdownlistSqlConnection con=new SqlConnection("server=(local);database=department;uid=sa;pwd=");
ListBox绑定数据库两种方法 dropdownlistcon.Open( );
ListBox绑定数据库两种方法 dropdownlistSqlCommand cm
=new SqlCommand("select empID,empName from emp",con);
ListBox绑定数据库两种方法 dropdownlistSqlDataReader dr
=cm.ExecuteReader( );
ListBox绑定数据库两种方法 dropdownlist
while(dr.Read( ))
con.Close( );

3.  为DropDownList添加第一项

ListBox绑定数据库两种方法 dropdownlist  this.DropDepartment.Items.Insert(0,new ListItem("请选择部门"));
ListBox绑定数据库两种方法 dropdownlist

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-20
  • 2021-06-12
  • 2021-09-28
猜你喜欢
  • 2022-03-08
  • 2021-10-06
  • 2022-12-23
  • 2021-07-11
  • 2022-12-23
相关资源
相似解决方案