比如,以下程序段:
procedure TForm1.Button2Click(Sender: TObject);
var
astr: string ;
begin
 adoquery1.Close ;
 adoquery1.SQL.Clear;
 astr:='select * from table1 where field1 like '%王%' ';
 adoquery1.SQL.add(astr);
 try
   adoquery1.Open ;
 except
   adoquery1.ExecSQL;
 end;
 end;
 end.    为了查询字段中含有“王”的记录,都是单引号,系统没办法区分!
    我用的是sql server 2000.

解决方案 »

  1.   

    整条sql语句与like子句的'%王%'的单引号重复,该怎么解决?
      

  2.   

    astr:='select * from table1 where field1 like ''%王%'' '
      

  3.   

    astr:='select * from table1 where field1 like '''+%王%'''';
    或者干脆这样:
    astr:='select * from table1 where field1 like '+''''+'%王%'+'''';
      

  4.   

    astr:='select * from table1 where field1 like ' + QuotedStr('%王%')
      

  5.   

    procedure TForm2.FlatEdit1KeyPress(Sender: TObject; var Key: Char);
    begin
        if key=#13 then
           begin
           if flatedit1.Text='' then
           application.MessageBox('您没有输入关键字!','给您的提示',mb_ok+MB_ICONEXCLAMATION )
           else
           adoquery1.Close;
           adoquery1.SQL.Clear;
           adoquery1.SQL.Add('select * from 内容 where 内容 like :aa');
           adoquery1.Parameters.ParamByName('aa').Value:='%'+flatedit1.Text+'%';
           adoquery1.Open;
           showmessage('找到满足条件的记录数:  '+inttostr(adoquery1.RecordCount)+'条');
           flatedit1.Text:='';
           end;
    end;