数据库中有个表,表的字段有学号和姓名等,C#中比如在textbox1中输入学号,在textbox2中可以显示出对应的姓名等信息。我的代码是这样的,但是有问题:
 private void button1_Click(object sender, EventArgs e)
        {
            string con="Data Source=GPKEBEQV7JLD6VE\\SQL;Initial Catalog=事务数据库;Integrated Security=True";
            SqlConnection cn = new SqlConnection(con);
            string a = textBox1.Text;
            string sql = " select 姓名 from 学生表 where 学号 ='" + a + "'";        
            cn.Open();
            textBox2.Text = sql;
        }
这样写的话textbox2中显示的只是sql代表的那条查询语句而不是查询的结果,要怎么写才能在textbox2中显示查询出来的结果呢?求高手帮忙,谢谢!

解决方案 »

  1.   

    可能需要加一个sqlCommand 
    然后sqlCommand 执行后,返回查询结果...
      

  2.   

      string con="Data Source=GPKEBEQV7JLD6VE\\SQL;Initial Catalog=事务数据库;Integrated Security=True";
                SqlConnection cn = new SqlConnection(con);
                string a = textBox1.Text;
                string sql = " select 姓名 from 学生表 where 学号 ='" + a + "'";        
                cn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn); 
                 SqlDataAdapter sda = new SqlDataAdapter(cmd); 
                 DataTable dt = new DataTable(); 
                 sda.Fill(dt); 
                cn.Close();
                cmd.Dispose(); 
                textBox2.Text =dt.rows[0][列名];;
      

  3.   

    你把这个SQL执行的结果绑定到别的控件上,比如GridView,显示到这个控件上面问题就变通了
      

  4.   

    你这得到的肯定是那段SQL语句啊,你得执行出结果然后把结果赋给textBox2.Text