2008-10-23 星期四
使用TADOTable组件模糊过滤:
1、使用FilterObjects属性设置过滤方式:
- //*************************************
- //* FilterObjects(TDataSet)属性原型:
- //*************************************
- Unit DB
- type
- TFilterOption = (foCaseInsensitive, foNoPartialCompare);
- TFilterOptions = set of TFilterOption;
- property FilterOptions: TFilterOptions;
- //*************************************
- //* 其中foCaseInsensitive表示忽略字母大小写,
- //* foNoPartialCompare表示不可以局部匹配,即
- //* 把Filter字符串中的星号(*)当成一个字符,否
- //* 则当成一个掩码字符。
- //*************************************
- //*************************************
- //* 演示使用FilterObjects属性设置过滤方式
- //*************************************
- with DM.adotDemo do
- begin
- Filtered := False;
- Filter := sFilter; //Filter过滤字符串,例如:Company='*S*'
- if rbNoPartialCompare.Checked then
- begin
- FilterOptions := FilterOptions + [foNoPartialCompare];
- end
- else
- begin
- FilterOptions := FilterOptions - [foNoPartialCompare];
- end;
- Filtered := True;
- end;
在SQL2005 Express数据库上结果报错:"FilterObjects are not supported!",后来在db数据库上编译通过
猜测:SQL2005 Express不支持这种语法
ps:Filter属性中,将星号(*)放在字符串首位,有时支持,有时报错,怀疑是Delphi的bug
2、使用Filter属性中的like操作符和百分号(%)掩码过滤:
- with DM.adotBook do
- begin
- if cbNoPartialCompare.Checked then
- begin
- sFilter := sFilter + '=' + QuotedStr(edtKey.Text);
- end
- else
- begin
- sFilter := sFilter + ' like ' + QuotedStr('%' + edtKey.Text + '%');
- end;
- Filtered := False;
- Filter := sFilter;
- Filtered := True;
- end;
ps:同样的,在过滤字符串首位加百分号(%),有时通过,有时报错,网上说是Delphi的bug,要安装一个补丁,有些D版不能打,待证实!
经验总结:使用ADOTable的Filtered代替SQL的where语句查询,可以增加查询性能