下拉列表与SQL数据库绑定

问题描述:

如何在断开连接的模式下绑定下拉列表

how to bind dropdownlist in disconnected mode

DataTable table = new DataTable();
           cmd = new SqlCommand();
           cmd.CommandText = "GetEmployee";
           cmd.CommandType = CommandType.StoredProcedure;

           table = dbConn.ExecuteQuery(cmd);
           ddlEmp.DataSource = table;
           ddlEmp.DataValueField = "DeptId";
           ddlEmp.DataTextField = "DeptName";
           ddlEmp.DataBind();


try
{
sqlconnection con = new sqlconnection(数据源= .;初始目录=数据库名称; uid = sa; pwd =密码");
con.Open();
sqlcommand cmd = new OleDbCommand(从购买中选择与众不同(prod_Name)",con);
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
comboBox1.Items.Add(dr [0] .ToString());
}
dr.Close();
con.Close();
}
catch(ex ex例外)
{
MessageBox.Show("Error ...." + ex.Message);
}
try
{
sqlconnection con=new sqlconnection("Data Source=.; Initial Catalog=database name;uid=sa; pwd=password");
con.Open();
sqlcommand cmd = new OleDbCommand("select distinct(prod_Name) from purchase" , con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr[0].ToString());
}
dr.Close();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error...."+ex.Message);
}


SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=cmcdb;Integrated Security=True");
        con.Open();
       SqlCommand cmd = new SqlCommand("select userid from logins ", con);
       SqlDataReader dr = cmd.ExecuteReader();
       while (dr.Read())
       {

           DropDownList1.Items.Add(dr["userid"].ToString());
       }