private void button1_Click(object sender, EventArgs e)
        {
            string ConStr = @"Integrated Security=SSPI;database=学生管理;data source=(local)";
            SqlConnection SqlCon = new SqlConnection(ConStr);
            string SelectCmd = "select * from student where" + comboBox1.Text + "like \'%" + textBox1.Text + "%\'";
            //string SelectCmd = "select * from student";
            SqlCommand SqlCmd = new SqlCommand(SelectCmd,SqlCon);
            DataSet DS = new DataSet();
            try
            {
                SqlCon.Open();
                SqlDataAdapter DA = new SqlDataAdapter(SqlCmd);
                DA.Fill(DS, "T1");
                dataGridView1.DataSource = DS.Tables["T1"].DefaultView;
            }
            catch (Exception ex) { }
            finally
            {
                SqlCon.Close();
            }
        }
string SelectCmd = "select * from student where" + comboBox1.Text + "like \'%" + textBox1.Text + "%\'";
请问这段代码有什么问题,怎么我显示不出结果呢,?
//string SelectCmd = "select * from student";
这个就可以执行出结果,
还有返回的结果不是在我原来已经编辑好的列里面显示的,而是重新新建的列,请问一下高手,应该怎么处理,!

解决方案 »

  1.   

    "select * from student where" + comboBox1.Text + "like \'%" + textBox1.Text + "%\'";
    "select * from student where " + comboBox1.Text + " like \'%" + textBox1.Text + "%\'";
    sql字符串构造要注意空格,先试试
      

  2.   

    string SelectCmd = "select * from student where" + comboBox1.Text + "like \'%" + textBox1.Text + "%\'";
    该为:string SelectCmd = "select * from student where" + comboBox1.Text + "like '%" + textBox1.Text + "%'";
    确保comboBox1.Text 的值是你数据库的字段
      

  3.   

    不好意思,忘记注意空格了
    string SelectCmd = "select * from student where " + comboBox1.Text + " like '%" + textBox1.Text + "%'";楼主可以跟踪下sql语句就知道怎么改了