比如有一张SHEET:
id   name 
1    wang
2    zhang需要实现:
在TEXTBOX1 输入 'wang',在textbox2或者DropDownList1相应的给出绑定的id,即1。问题:
1、用textbox2还是DropDownList1能够比较方便的实现?
2、protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        //咋个整 ?? 
    }
谢了!

解决方案 »

  1.   

    只是要显示id的话用TextBox就可以啦
    再加一个Button,在它的Click事件中写using(SqlConnection sc = new SqlConnection("连接字符串"))
    {
         sc.Open();
         SqlCommand cmd = new SqlCommand("select id from SHEET where name='" + TextBox1.Text + "'", sc);
         SqlDataReader sdr = cmd.ExecuteReader();
         if(sdr.HasRows)
             TextBox2.Text=sdr["id"].ToString();
         sdr.Close();
    }
      

  2.   

    LS少写了一句,
    if(sdr.HasRows)
           if(sdr.Read())
             TextBox2.Text=sdr["id"].ToString();
         sdr.Close();
      

  3.   

    using (SqlConnection sc = new SqlConnection("连接字符串"))
    {
        SqlCommand cmd = new SqlCommand("select id from SHEET where name= @name", sc);
        cmd.Parameters.AddWithValue("@name ", TextBox1.Text);
        sc.Open();
        SqlDataReader sdr = cmd.ExecuteReader();
        if (sdr.Read())
            TextBox2.Text = sdr["id"].ToString();
        sdr.Close();
    }