procedure TForm3.Button1Click(Sender: TObject);
var sqlstr:string;
begin
adoquery1.Edit;
sqlstr:='select * from userinfo';
if (combobox1.Text='精确查询') and (edit1.Text<>'') then
begin
 //adoquery1.SQL.Text:='select * from  userinfo where username='''+edit1.Text+''' and  sex='''+combobox2.Text+''' and  department='''+combobox3.Text+'''' ;  sqlstr:=sqlstr+'where and username='+quotedstr(edit1.text);
  if combobox2.Text<>'' then  sqlstr:=sqlstr+'and sex='+ quotedstr(combobox2.text);  if combobox3.Text<>'' then
  begin
  sqlstr:=sqlstr+'and department='+quotedstr(combobox3.text);
  end;
  ADOQuery1.Close ;
  ADOQuery1.SQL.Clear ;
  ADOQuery1.SQL.Add(sqlstr);
  ADOQuery1.Open ;  if adoquery1.RecordCount=0  then  showmessage('没有符合的记录')
  else
   showmessage('纪录已经查找到了');
   edit1.text:='';
  end;
if combobox1.Text='模糊查询' then
   ADOQuery1.Close ;
   ADOQuery1.SQL.Clear ;
   ADOQuery1.SQL.Add('select * from userinfo');
   ADOQuery1.SQL.Add('where username like'''+edit1.text+'%''');
   ADOQuery1.Open ;

解决方案 »

  1.   

    应该是sql语句问题,你可以使用ShowMessage(ADOQuery1.sql.text);来查看sql代码是否有问题。
      

  2.   

    SQL语句缺少空格,你跟踪一下就可以发现了,DELPHI中写SQL要养成一种加空格的习惯。
    你是这样写的:
    sqlstr:='select * from userinfo';
    if (combobox1.Text='精确查询') and (edit1.Text<>'') then
    begin
     //adoquery1.SQL.Text:='select * from userinfo where username='''+edit1.Text+''' and sex='''+combobox2.Text+''' and department='''+combobox3.Text+'''' ;  sqlstr:=sqlstr+'where and username='+quotedstr(edit1.text);最后一句应该是:
      sqlstr:=sqlstr+' where username='+quotedstr(edit1.text);where的前面加个空格,而且where后面怎么会有and呢?好好查一下SQL语句
      

  3.   


    是多了一and,
    提議delphi加個memo1 show sql語 . 然後copy到sql中看是否通過.
    memo1.txt=adoquery1.sql.text;
      

  4.   

    这种情况可以加个ShowMessage,或者直接caption显示等方法,把语句显示出来看,那样就清晰很多,一下就看到错在哪
      

  5.   

    sqlstr:=sqlstr+' where username='+quotedstr(edit1.text);
      

  6.   

    你的这个问题是一个多条件查询的问题,也就是你的where 该放哪里的问题。看你的代码这里if (combobox1.Text='精确查询') and (edit1.Text<>'') then
    begin
     //adoquery1.SQL.Text:='select * from userinfo where username='''+edit1.Text+''' and sex='''+combobox2.Text+''' and department='''+combobox3.Text+'''' ;  sqlstr:=sqlstr+'where and username='+quotedstr(edit1.text);
      if combobox2.Text<>'' then  sqlstr:=sqlstr+'and sex='+ quotedstr(combobox2.text);
    如果第一个条件不成立,也就是说,你后面的条件都缺少一个关键字  where ,除非所有条件都不成立,只要除第一个条件不成立,这个语句就会出错。处理方法很简单在这里改一下
    adoquery1.Edit;
    sqlstr:='select * from userinfo where 1=1';
    后面的条件判断完后直接写sqlstr := sqlstr+' and .......'
    就OK了