string strconn = "server=localhost;uid=sa;pwd=sa;database=购物";
        SqlConnection cn = new SqlConnection(strconn);
        string mysql = "select * from Product,Price where Product.PID=Price.PPID";
        if (ddl_pgroup.SelectedIndex.ToString() != "0")
        {
            mysql = mysql + "and pgroup=" + Convert.ToInt16(ddl_pgroup.SelectedIndex.ToString());
        }
        if (tbx_pid.Text.ToString() != "")
        {
            mysql = mysql + "and PID=" + Convert.ToInt16(tbx_pid.Text.ToString());
        }
        
        if (tbx_pname.Text.ToString()!="") 
        {
            mysql = mysql + "and pname like'%" + tbx_pname.Text.ToString() + "%'";
        }
        if (tbx_pvender.Text.ToString() != "")
        {
            mysql = mysql + "and pvender like '%" + tbx_pvender.Text.ToString() + "%'";
        }
        
        SqlDataAdapter da = new SqlDataAdapter(mysql, cn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        dgd_plist.DataSource = ds;
        dgd_plist.DataBind();
        cn.Close();
这是一段搜索的代码,但是每次运行的时候,只要用任何的内容来搜索总是出现错误。比如用pname来搜索,就出现“'pname' 附近有语法错误”的问题,哪位高手能指点一下啊,谢谢

解决方案 »

  1.   

    string mysql = "select * from Product,Price where Product.PID=Price.PPID";
    ==========================================================================
    string mysql = "select * from Product,Price where Product.PID=Price.PPID ";后面先加一个空格,否则拼接肯定错误
      

  2.   

    mysql = mysql + "and pgroup=" + Convert.ToInt16(ddl_pgroup.SelectedIndex.ToString());
    mysql = mysql + "and PID=" + Convert.ToInt16(tbx_pid.Text.ToString());
    mysql = mysql + "and pname like'%" + tbx_pname.Text.ToString() + "%'";
    mysql = mysql + "and pvender like '%" + tbx_pvender.Text.ToString() + "%'";这四条语句在and前面都加个空格
      

  3.   

    mysql = mysql + "and pname like'%" + tbx_pname.Text.ToString() + "%'";
    ======================================================================like和'之间加个空格错误估计就这么多了吧,不过还有地方写的不是很好...
      

  4.   

    顶LS上的,拼接SQL语句时,空格一定要注意!
      

  5.   

    string strconn = "server=localhost;uid=sa;pwd=sa;database=购物";
            SqlConnection cn = new SqlConnection(strconn);
            string mysql = "select * from Product,Price where Product.PID=Price.PPID";
            if (ddl_pgroup.SelectedIndex.ToString() != "0")
            {
                mysql += " and pgroup=" + Convert.ToInt16(ddl_pgroup.SelectedIndex.ToString());
            }
            if (tbx_pid.Text.ToString() != "")
            {
                mysql += " and PID=" + Convert.ToInt16(tbx_pid.Text.ToString());
            }
            
            if (tbx_pname.Text.ToString()!="") 
            {
                mysql += " and pname like '%" + tbx_pname.Text.ToString() + "%'";
            }
            if (tbx_pvender.Text.ToString() != "")
            {
                mysql += " and pvender like '%" + tbx_pvender.Text.ToString() + "%'";
            }
            
            SqlDataAdapter da = new SqlDataAdapter(mysql, cn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dgd_plist.DataSource = ds;
            dgd_plist.DataBind();
            cn.Close();
      

  6.   

    把 SQL 语句输出看一下,一切就都明白了呀!