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 ;
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 ;
你是这样写的:
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语句
是多了一and,
提議delphi加個memo1 show sql語 . 然後copy到sql中看是否通過.
memo1.txt=adoquery1.sql.text;
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了