Delphi基础学习

来源:互联网 发布:sql partition by 编辑:程序博客网 时间:2024/05/19 04:53

2008-10-23 星期四

使用TADOTable组件模糊过滤:

1、使用FilterObjects属性设置过滤方式:

  1. //*************************************
  2. //* FilterObjects(TDataSet)属性原型:
  3. //*************************************
  4. Unit DB
  5. type
  6.   TFilterOption = (foCaseInsensitive, foNoPartialCompare);
  7.   TFilterOptions = set of TFilterOption;
  8. property FilterOptions: TFilterOptions;
  9. //*************************************
  10. //* 其中foCaseInsensitive表示忽略字母大小写,
  11. //* foNoPartialCompare表示不可以局部匹配,即
  12. //* 把Filter字符串中的星号(*)当成一个字符,否
  13. //* 则当成一个掩码字符。
  14. //*************************************
  15. //*************************************
  16. //* 演示使用FilterObjects属性设置过滤方式
  17. //*************************************  
  18.   with DM.adotDemo do
  19.   begin
  20.     Filtered := False;
  21.     Filter := sFilter;  //Filter过滤字符串,例如:Company='*S*'
  22.     if rbNoPartialCompare.Checked then
  23.     begin
  24.       FilterOptions := FilterOptions + [foNoPartialCompare];
  25.     end
  26.     else
  27.     begin
  28.       FilterOptions := FilterOptions - [foNoPartialCompare];
  29.     end;
  30.     Filtered := True;
  31.   end;

在SQL2005 Express数据库上结果报错:"FilterObjects are not supported!",后来在db数据库上编译通过

猜测:SQL2005 Express不支持这种语法

ps:Filter属性中,将星号(*)放在字符串首位,有时支持,有时报错,怀疑是Delphi的bug

2、使用Filter属性中的like操作符和百分号(%)掩码过滤:

  1.   with DM.adotBook do
  2.   begin
  3.     if cbNoPartialCompare.Checked then
  4.     begin
  5.       sFilter := sFilter + '=' + QuotedStr(edtKey.Text);
  6.     end
  7.     else
  8.     begin
  9.       sFilter := sFilter + ' like ' + QuotedStr('%' + edtKey.Text + '%');
  10.     end;
  11.     Filtered := False;
  12.     Filter := sFilter;
  13.     Filtered := True;
  14.   end;

ps:同样的,在过滤字符串首位加百分号(%),有时通过,有时报错,网上说是Delphi的bug,要安装一个补丁,有些D版不能打,待证实!

经验总结:使用ADOTable的Filtered代替SQL的where语句查询,可以增加查询性能