使用LIKE和foCaseInsensitive的Delphi TClientDataSet过滤 [英] Delphi TClientDataSet Filtering using LIKE and foCaseInsensitive
问题描述
使用Delphi XE,我正在尝试对TClientDataSet进行一些过滤,但无法使不区分大小写的过滤与LIKE运算符一起正常工作
Using Delphi XE, I'm trying to do some filtering on a TClientDataSet and I'm unable to get the case-insensitive filtering to work properly in conjunction with a LIKE operator
考虑以下代码进行过滤
cdsDocs.DisableControls;
try
cdsDocs.Filtered := False;
cdsDocs.FilterOptions := [foCaseInsensitive];
cdsDocs.Filter := 'Product LIKE ''%' + txtFilter.Text + '%''';
cdsDocs.Filtered := True;
finally
cdsDocs.EnableControls;
end;
,并认为此数据集的产品"字段仅包含值"b"和"B".
and consider that the Product field of this dataset contains only the values 'b' and 'B'.
- 当(txtFilter.Text ='b')时,我只会得到'b'(我期望是'b'和'B')
- 当(txtFilter.Text ='B')时,我只会得到'B'(同样,我期望的是'b'和'B')
好像LIKE'%b%'和foCaseInsensitive不能一起使用?我应该怎么做才能使其正常工作?我阅读了文档,但找不到错误(如果有的话). TIA.
It seems as though the LIKE '%b%' and the foCaseInsensitive don't work together? What should I do to make it work? I read the documentation but I can't find my mistake (if any). TIA.
推荐答案
一切正常,直到我尝试扩展筛选以允许不区分大小写的搜索(我尝试使用现有的FilterOption foCaseInsensitive),现在我突然有了一个错误?不,对我而言这没有意义.我决定以另一种方式实现所需的不区分大小写的过滤,并保持我的自我完整.
Everything was working fine until I tried to extend the filtering to allow for case-insensitive searches (I tried to use the existing FilterOption foCaseInsensitive), now suddenly I have a bug? No, that doesn't make sense to me. I've decided to achieve the desired case-insensitive filtering another way, and keep my ego intact.
这是经过修改的代码(可以正常工作)
Here's the revised code (works perfectly)
cdDocs.DisableControls;
try
cdDocs.Filtered := False;
cdDocs.FilterOptions := [];
if (cbCaseSensitive.Checked) then
cdDocs.Filter := 'Product LIKE ''%' + txtFilter.Text + '%'''
else
cdDocs.Filter := 'UPPER(Product) LIKE ''%' + UPPERCASE(txtFilter.Text) + '%''';
cdDocs.Filtered := True;
finally
cdDocs.EnableControls;
end;
顺便说一句,这是使用Delphi XE附带的Interbase DB Access组件连接到Interbase/Firebird数据库的.如果连接到其他数据库,则 可能需要用所选RDBMS的等效函数替换"UPPER"函数(无论在等效SQL WHERE子句中使用什么)
BTW, this is connecting to an Interbase/Firebird database using the Interbase DB Access components that ship with Delphi XE. If connecting to a different DB you may need to replace the "UPPER" function with the equivalent function for your chosen RDBMS (whatever you would use in the equivalent SQL WHERE clause)
这篇关于使用LIKE和foCaseInsensitive的Delphi TClientDataSet过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!