请教一下public void BindPlan()
{
string mySelectQuery = "select class_id,class_name from class";

SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

while (myReader.Read())
{
ListBox1.Items.Add(myReader.GetString(1));

}
myReader.Close(); }
绑定数据到ListBox1上,怎样将id(自动增长)绑定到
ListBox1的values属性上和将name字段绑定到ListBox1的text属性上??

解决方案 »

  1.   

    public void BindPlan()
    {
    string mySelectQuery = "select class_id,class_name from class";
    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

    SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
    myConnection.Open();
    SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

    while (myReader.Read())
    {
    ListItem item=new ListItem(myReader.GetString(1),myReader.GetString(0));
    ListBox1.Items.Add(item);
    }
    myReader.Close();
    this.ListBox1.DataTextField="class_name";
    this.ListBox1.DataValueField="class_id";
    }
      

  2.   

    你上面的不叫邦定吧string mySelectQuery = "select class_id,class_name from class";

    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);System.Data.DataSet ds=new DataSet();
    System.Data.SqlClient.SqlDataAdapter da=new System.Data.SqlClient.SqlDataAdapter(mySelectQuery,myConnection);
    da.Fill(ds);this.ListBox1.DataSource=ds.Tables[0];
    this.ListBox1.DataTextField="class_name";
    this.ListBox1.DataValueField="class_id";
    this.ListBox1.DataBind();