如果您使用的是 DropDownList(此处命名为 yourDdl)并且您想用来自数据库的数据填充它,我建议您执行以下操作:
yourDdl.DataSource = getListSelection("AtecoYear = 2004", true);
yourDdl.DataTextField = "ComboText";
yourDdl.DataValueField = "ComboID";
yourDdl.DataBind();
getListSelection 方法(最好的方法是将它放在适当的类中)连接到您的数据库,提取数据并将其放入 DataTable:
public DataTable getListSelection(string filter_status)
{
DataTable dt;
string cmdText = "select ExampleID as ComboID, ExampleName as ComboText from ExampleTable";
SqlConnection objIdbConnection = new SqlConnection(yourConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = cmdText;
cmd.Connection = objIdbConnection;
cmd.CommandTimeout = 30; // seconds
cmd.Connection.Open();
IDataAdapter da = new SqlDataAdapter((SqlCommand)cmd);
DataSet myDataTable = new DataSet();
da.Fill(myDataTable);
objIdbConnection.Close();
return myDataTable.Tables[0];
}