用checkbox控件打算做一个多选择查询问题,单个选项选择的查询已经实现。但是当选择2个或多个选项的查询时,Fill却一直报错“语法错误(操作符丢失)”在查询表达式请哪位高手能给指点下,看我2个选项以上的查询语句错在哪里了,谢谢。代码如下:
 if (cb1.Checked)
            {
                queryselect += "model" + "like '%" + cbbcategory.Text + "%'";
            }
            if (cb2.Checked)
            {
                queryselect += "modelnumber" + " like '%" + txtMN.Text + "%'";
            }
            if (cb3.Checked)
            {
                queryselect += "problemdescribe" + " like '%" + cbbQC.Text + "%'";
            }
            if (cb4.Checked)
            {
                queryselect += "author" + " like '%" + txtAuthor.Text + "%'";
            }
            if (cb1.Checked && cb2.Checked)
            {
                queryselect += "model" + " like '%" + cbbcategory.Text + "%'" + " and" + " modelnumber" + " like '%" + txtMN.Text + "%'";
            }
            if (cb1.Checked && cb3.Checked)
            {
                queryselect += "model" + "like'&" + cbbcategory.Text + "&'" + " and" + "problemdescribe" + "like'" + cbbQC.Text + "%'";
            }
            if (cb1.Checked && cb4.Checked)
            {
                queryselect += "model" + " like '%" + cbbcategory.Text + "%'" + " and" + " author" + " like '%" + txtAuthor.Text + "%'";
            }
            if (cb2.Checked && cb3.Checked)
            {
                queryselect += "modelnumber" + " like '%" + txtMN.Text + "%'" + " and" + " problemdescribe" + " like '%" + cbbQC.Text + "%'";
            }
            if (cb2.Checked && cb4.Checked)
            {
                queryselect += "modelnumber" + " like '%" + txtMN.Text + "%'" + " and" + " author" + " like '%" + txtAuthor.Text + "%'";
            }
            if (cb3.Checked && cb4.Checked)
            {
                queryselect += " problemdescribe" + " like '%" + cbbQC.Text + "%'" + " and" + " author" + " like '%" + txtAuthor.Text + "%'";
            }
            if (cb1.Checked && cb2.Checked && cb3.Checked)
            {
                queryselect += "model" + " like '%" + cbbcategory.Text + "%'" + " and" + "modelnumber" + " like '%" + txtMN.Text + "%'" + " and" + " problemdescribe" + " like '%" + cbbQC.Text + "%'";
            }
            if (cb1.Checked && cb2.Checked && cb4.Checked)
            {
                queryselect += "model" + " like '%" + cbbcategory.Text + "%'" + " and" + "modelnumber" + " like '%" + txtMN.Text + "%'" + " and" + " author" + " like '%" + txtAuthor.Text + "%'";
            }
            if (cb2.Checked && cb3.Checked && cb4.Checked)
            {
                queryselect += "modelnumber" + " like '%" + txtMN.Text + "%'" + " and" + " problemdescribe" + " like '%" + cbbQC.Text + "%'" + " and" + "author" + " like '%" + txtAuthor.Text + "%'";
            }
            if (cb1.Checked && cb2.Checked && cb3.Checked && cb4.Checked)
            {
                queryselect += "model" + " like '%" + cbbcategory.Text + "%'" + " and" + "modelnumber" + " like '%" + txtMN.Text + "%'" + " and" + " problemdescribe" + " like '%" + cbbQC.Text + "%'" + " and" + "author" + " like '%" + txtAuthor.Text + "%'";
            }
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source=|DataDirectory|\dbjt.mdb");
            conn.Open();
da.Fill(ds, "jt")

解决方案 »

  1.   

    前面应该有个空格,你完全可以打个断点,然后看最终拼接的语句是否有问题
    并且每个条件应该 and 连接吧
    queryselect += "model" + "like '%" + cbbcategory.Text + "%'"; string query = "select * from where 1=1";                if (true)
                    {
                        query += " and Name='xiaowang'";
                    }
                    if (true)
                    {
                        query += " and Age>20";
                    }
      

  2.   

    string queryselect="select * from table where 1=1" 
    if (cb1.Checked)
                {
                    queryselect += "and model" + "like '%" + cbbcategory.Text + "%'";
                }
    if (cb2.Checked)
                {
                    queryselect += "and modelnumber" + " like '%" + txtMN.Text + "%'";
                }
    if (cb3.Checked)
                {
                    queryselect += "and problemdescribe" + " like '%" + cbbQC.Text + "%'";
                }
    if (cb4.Checked)
                {
                    queryselect += "and author" + " like '%" + txtAuthor.Text + "%'";
                }
    后面那些就没用了  
      

  3.   

    and  like  要加空格 基本上每个+分割的字符串 你都加空格就不会错
      

  4.   

    你的判断逻辑本身有问题,多个IF条件都会进入,自然sql字符串拼接错误,正解同3楼。
      

  5.   

    and 前面都+空格你好,我按你的方法修改代码后还是出现同样的错误,且单个查询都出这种错误,请问该怎么修改呢?谢谢
      

  6.   

    而我把and去掉后,可进行单个查询,但是多项查询还是出现“语法错误(操作符丢失)”在查询表达式
      

  7.   

    调试看看最终执行的sql语句是什么?
      

  8.   

    楼主之前单独check可以执行,sql语句的最后应该是个where,所以拼接下面的一条语句没有问题,但拼接多条没有and连接,所以报语法错误
    把where改成where 0=0
    后面check里面的sql前面都加上and
    逻辑问题就像楼上说的,只留前4个就行
      

  9.   

    多谢楼上各位,按你们的方法以及调出来了。谢谢。还有个问题,where后的1=1,和,0=0.是什么意思呢?
      

  10.   

    where 0=0 是为了下面好加 and    要是不加0=0等语句 后面的条件第一个满足的就不能加and了 第二个开始要加and   不能确定哪个条件满足 所以 加上where 0=0 后面全部加上and 
      

  11.   

    where 0=0 是为了下面好加 and    要是不加0=0等语句 后面的条件第一个满足的就不能加and了 第二个开始要加and   不能确定哪个条件满足 所以 加上where 0=0 后面全部加上and 不管哪个条件满足 SQL语句都没问题