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:=sqlstr+' where and username='+quotedstr(edit1.text);
case combobox1.itemsindex of
0:begin ... end;
1:begin ... end;
这样貌似条理清楚点
'select * from userinfo where username='+''''+edit1.Text+''''+' and sex='+''''+combobox2.Text+'''' +'and department='+''''+combobox3.Text+'''' ;其他的写法类似。
ADOQuery1.Close ;
ADOQuery1.SQL.Clear ;
ADOQuery1.SQL.Add(sqlstr);
ADOQuery1.Open ;
这句话。。你试试在sqlstr两边加引号或者是ADOQUERY1.SQL.TEXT:='sqlstr';还有函数 QuotedStr把字符串S转换成为引用的字符串。单引号" ' " 将被插入到字符串s的最前和最后。你这里quotedstr(edit1.text)运行后应该是'edit1.text'而在delphi的SQL语句中引用输入框的数据应该是'''+edit.text+''' 你试试我也是新手。大家互相学习哈
请问where之后为什么要加and,
sqlstr:=sqlstr+' where username='+quotedstr(edit1.text);这样就ok。