DropDownLis t连接数据库
string strCnn = "data source=.;initial catalog=students;integrated security=true";
SqlConnection sqlCnn = new SqlConnection(strCnn);
SqlCommand sqlCmm = new SqlCommand();
sqlCmm.Connection = sqlCnn;
sqlCmm.CommandText = "SELECT id,name FROM city WHERE provinceid=@pid";
SqlParameter param = new SqlParameter("@pid", System.Data.SqlDbType.Int);
param.Value = this.DropDownList1.SelectedValue;
sqlCmm.Parameters.Add(param);
SqlDataReader reader = null;
try{
sqlCnn.Open();
reader = sqlCmm.ExecuteReader();
this.DropDownList2.DataSource = reader;
this.DropDownList2.DataTextField = "name";
this.DropDownList2.DataValueField = "id";
this.DropDownList2.DataBind();
}
catch{throw;}
finally{
reader.Close();
sqlCnn.Close();
}