以下是ListBox和DropDownList数据绑定的方法,因为两种绑定的方式基本差不多,所以用ListBox举例:
private void Button1_Click(object sender, System.EventArgs e)
{
if(ListBox1.Visible==false)
{
SqlConnection conn=new SqlConnection(@"Server="+DB_Host+";DataBase="+DB_Name+";uid="+DB_Uid+";pwd="+DB_Pwd+"");
SqlDataAdapter da=new SqlDataAdapter("SELECT * from TEMP",conn);
DataSet ds=new DataSet();
da.Fill(ds,"UN");
ListBox1.DataSource=ds;
ListBox1.DataTextField="UN";
ListBox1.DataValueField="UP";
ListBox1.DataBind();
ListBox1.Visible=true;
}
else
{
ListBox1.Visible=false;
}
}