protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        SqlConnection libCon = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=library;Integrated Security=True");
        libCon.Open();
        string BorSQL;
        BorSQL = "select rName from borrow where bookid = '"+TextBox1.Text+"';";
        SqlCommand StuIns = new SqlCommand(BorSQL, libCon);
        SqlDataReader libDa = StuIns.ExecuteReader();
        libDa.Read();
        Response.Write(label1.Text =
        libCon.Close();
       
************************************************************************************************************
我想从通过TextBox1.Text输入,从borrow表里查询到rName 并输入到label1里面

解决方案 »

  1.   


       
        string sql = " select rName from borrow where bookid =@bookid";
        using (SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=library;Integrated Security=True"))
        {
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add("@bookid", SqlDbType.VarChar);
            cmd.Parameters["@name"].Value = TextBox1.Text;
            try
            {
                conn.Open();
                label1.Text= cmd.ExecuteScalar()==null?"":cmd.ExecuteScalar().ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        
      

  2.   

     cmd.Parameters.Add("@bookid", SqlDbType.VarChar);
            cmd.Parameters["@bookid"].Value = TextBox1.Text; //囧  更正
      

  3.   


    protected void TextBox1_TextChanged(object sender, EventArgs e)
      {
      using(SqlConnection libCon = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=library;Integrated Security=True"))
      libCon.Open();
      string BorSQL;
      BorSQL = "select rName from borrow where bookid = '"+TextBox1.Text+"'";
      using(SqlCommand StuIns = new SqlCommand(BorSQL, libCon))
      {
           using(SqlDataReader libDa = StuIns.ExecuteReader())
           {
              while(libDa.Read())
             {
                label1.Text = libDa["rName"].ToString();
              }
            }
       }
    }