aa   =   "select   *   from   ceshi   where   "   +   this.DropDownList1.SelectedValue   +   "='"   +   this.TextBox1.Text.Trim()   +"'"; 
aa   =   "select   *   from   ceshi   where   '"   +   this.DropDownList1.SelectedValue   +   "'='"   +this.TextBox1.Text.Trim()   +"'"; 
aa   =   "select   *   from   ceshi   where   '"   +   this.DropDownList1.SelectedValue   +   "'=''"+this.TextBox1.Text.Trim()   +"''"; //数据库字段是文本类型
第一条是对的,但文本变量this.DropDownList1.SelectedValue好像要用''引起来才行,如第二句,为什么可以不引起来;但第二句查询不到,因为我的this.TextBox1.Text.Trim()只输入数字,好像也要用''引起来才行(不知对不对,但"select   *   from   ceshi   where     id='1'"是行的),如第三句;但第三句执行是错的,语法错误   (操作符丢失)   在查询表达式   ''id'=''1'''   中,这是什么问题?请高手帮忙!!请分析具体一些!

解决方案 »

  1.   

    aa       =       "select       *       from       ceshi       where      "       +       this.DropDownList1.SelectedValue       +       "='"+this.TextBox1.Text.Trim()       +"'";   
    第3行改成这样
      

  2.   

    作为数字查询的话是不用单引号引用的。aa =  "select   *   from  ceshi   where   '" +   this.DropDownList1.SelectedValue  +  "'=''"+this.TextBox1.Text.Trim() +"''";  
    如果是数字的话写作
    aa =  "select   *   from  ceshi   where   "' +   this.DropDownList1.SelectedValue  +  '"="+this.TextBox1.Text.Trim() ;  
    试试
    [/code]"select       *       from       ceshi       where           id='1'"是行的 
    这里的1是作为字符串处理的,不是数字
      

  3.   

    我的this.DropDownList1.SelectedValue不是数字
    aa =  "select   *   from  ceshi   where   '" +   this.DropDownList1.SelectedValue  +  "'=''"+this.TextBox1.Text.Trim() +"''";  我试了好像不对,我是问1楼的this.DropDownList1.SelectedValue(是文本变量)没有引起来,行吗?
      

  4.   

    你的this.DropDownList1.SelectedValue     传进去的值是作为字段名的吧````你想想啊
    "select       *       from     ceshi       where" +this.DropDownList1.SelectedValue等于什么?
    "select * from ceshi where id"是不是这个啊```
      

  5.   

    aa =  "select   *   from  ceshi   where   " +   this.DropDownList1.SelectedValue  +  "="+this.TextBox1.Text.Trim() ;
    理解错了,应该是这样吧,楼主试试
      

  6.   

    7楼说的对,但this.DropDownList1.SelectedValue毕竟是文本变量,按语法是要引起来的啊
      

  7.   

    我怎么感觉回复过这个帖子?
    你的第二第三种方法肯定是错误的,把where 后面的 用'引起来,那岂不是字符串和字符串比较了?
    为什么第二个查不出东西?你所比较的是两个字符串,加入你想得到的效果是ID='1',因为用到了',所以说成了:(设TextBox1.Text="1")
     ... where 'ID'='1'
    很显然,原本你的意思是数据库中ID这一字段是1的,给他选出来,但是现在成了字符串比较了,字符ID是和1不相等的,所以说加'是不对的。
    第一种应该是正确的,但是如果你数据库中所查询的字段的类型是数字,那么他的值就不能被'引起来。
    也就是说:如果数据库中ID是int型的,那么直接(程序中): "...where ID="+TextBox.Text.Trim();
      

  8.   

    这些问题老大估计不愿意看,老大喜欢有挑战的问题,不过偶页很菜。帮你顶,数据库设计有问题感觉,楼主的SQL语句应该考虑动态SQL语句
      

  9.   

    建议楼主换一种格式书写在后台书写sql语句:aa  = string.Format(@" select * from ceshi where [{0}] = '{1}' " ,
                           this.DropDownList1.SelectedValue,
                           this.TextBox1.Text.Trim() 
                         );如果这个字段的值是数字就不用加引号,字段最好用[]包起来