SqlConnection conn = new SqlConnection("server=.;uid=sa;database=pubs");
SqlDataAdapter dap = new SqlDataAdapter("select * from jobs", conn);
DataTable dt = new DataTable();
dap.Fill(dt);
DropDownList1.Items.Clear();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "job_desc";
DropDownList1.DataValueField = "job_id";
DropDownList1.DataBind();

这样就可以了。
方法二:
使用代码:
SqlConnection conn = new SqlConnection("server=.;uid=sa;database=pubs");
SqlDataAdapter dap = new SqlDataAdapter("select * from jobs", conn);
DataTable dt = new DataTable();
dap.Fill(dt);
DropDownList1.Items.Clear();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "job_desc";
DropDownList1.DataValueField = "job_id";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("", ""));//插入空项,此举必须放到数据绑定之后效果:
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-442002/ ,如需转载,请注明出处,否则将追究法律责任。