类型:转载 责任编辑:asp 日期:2007/03/01
本人用sql语句,加上if的判断条件来做的。在多个combobox中选择选项,以此(所选的项)做为条件,然后通过单击按钮来实现符合多个条件的或一个条件的查询,结果符合条件的记录显示在dbgrid中。
但在做当其中任何一个或多个combobox中为空值时(就是没有做出选择时),如何让sql语句来实现:不去读取该空值而继续判断已经做出选择的combobox,从而显示出符合条件的记录.
或是如果你有好的方法来完成多个条件的查询,也劳架你帮帮我,好吗?谢谢!!!!
推荐阅读
var sqlstr: string;
sqlstr := select * from tablename where 1=1 ;
if combobox1.itemindex <> -1 then
sqlstr := sqlstr + and a=+combobox1.items[combobox1.itemindex]+;
if combobox2.itemindex <> -1 then
sqlstr := sqlstr + and b=+combobox2.items[combobox2.itemindex]+;
.................
if comboboxn.itemindex <> -1 then
sqlstr := sqlstr + and n=+comboboxn.items[comboboxn.itemindex]+;
query1.close;
query1.sql.text := sqlstr;
query1.open;
with query1 do
begin
close;
sql.clear;
sql.add(select * from tablename where names=大虫子);
if combobox1.text<> then
sql.add(and params1=+combobox1.text+);
if combobox2.text<> then
sql.add(and params2=+combobox2.text+);
if combobox3.text<> then
sql.add(and params3=+combobox3.text+);
if combobox4.text<> then
sql.add(and params4=+combobox4.text+);
.......
open;
end;
另:还可以给变量设置 初始值,sql语句为固定式的!如果,该combobox.text为空,则取默认值。比如: aa between and zz