为什么下面两段代码,第一段运行报错,而第二段运行可以通过,为什么啊?
第一段
 SqlConnection conn = sqlconn.sqlconn1();
            conn.Open();
            string sql = "select * from XLImport5 where PO=" + this.textBox1.Text.ToString();
            SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "XLImport5");
            conn.Close();
            dataGridView1.DataSource = ds.Tables["XLImport5"];第二段 SqlConnection conn = sqlconn.sqlconn1();
            conn.Open();
            string sql = "select * from XLImport5 where PO='123' ";
            SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "XLImport5");
            conn.Close();
            dataGridView1.DataSource = ds.Tables["XLImport5"];

解决方案 »

  1.   

    string sql = "select * from XLImport5 where PO=" + this.textBox1.Text.ToString();上面代码应该这样写 ,你试试string sql = string.format("select * from XLImport5 where PO={0}",this.textBox1.Text.ToString());
      

  2.   

    string sql = "select * from XLImport5 where PO=" + this.textBox1.Text.ToString(); 
     错在这一句:
            this.textBox1.Text.ToString()   的两边均加单引号就可以。因为该字段不是数值型的,所以加跟不加是不一样的。
      

  3.   

    string sql = "select * from XLImport5 where PO='"+ this.textBox1.Text.ToString()+"'"; 
      

  4.   

     string sql = "select * from XLImport5 where PO='" + this.textBox1.Text.ToString()"'; 
    在双引号外面要套一个单引号