模糊查询的什么毛病? (100分)

  • 主题发起人 主题发起人 zjmg
  • 开始时间 开始时间
Z

zjmg

Unregistered / Unconfirmed
GUEST, unregistred user!
我用Paradox+DBgrid+Tquery+Datasource做模糊查询,需用Combobox2动态输入查询字段。
当Combobox2.text显示字符时,DBgrid1能正确显示出查询结果;而当Combobox2.text为空时,
则会显示出表中全部内容(这不是我想要的),请问如何避免这种问题?
代码:
procedure TForm12.Button2Click(Sender: TObject);
begin
Query1.close;
query1.sql.clear;
query1.sql.add('select *');
query1.sql.add('from zpb');
query1.sql.add('where 单位名称 like '''+'%'+combobox2.text+'%'+'''');
query1.prepare;
query1.open;
end;
 
加個語句不就可以了嗎?
if combobox1.text<>'' then
query1.sql.add('where 单位名称 like '''+'%'+combobox2.text+'%'+'''');
else
query1.sql.add('where 单位名称 = ''''');
 
if combobox2.text='' then exit;
 
同意楼上。
 
procedure TForm12.Button2Click(Sender: TObject);
begin
Query1.close;
query1.sql.clear;
query1.sql.add('select *');
query1.sql.add('from zpb');
if combobox2.text<>'' then
query1.sql.add('where 单位名称 like '''+'%'+combobox2.text+'%'+'''')
else
query1.sql.add('where 单位名称 like ''%''')
end;//if
query1.prepare;
query1.open;
end;
end;
 
同一楼上的几位
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
456
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
951
SUNSTONE的Delphi笔记
S
I
回复
0
查看
996
import
I
后退
顶部