gpt4 book ai didi

Delphi TClientDataSet 使用 LIKE 和 foCaseInsensitive 进行过滤

转载 作者:行者123 更新时间:2023-12-02 04:02:35 25 4
gpt4 key购买 nike

使用 Delphi XE,我尝试对 TClientDataSet 进行一些过滤,但无法使不区分大小写的过滤与 LIKE 运算符一起正常工作

考虑执行过滤的代码

cdsDocs.DisableControls;
try
cdsDocs.Filtered := False;
cdsDocs.FilterOptions := [foCaseInsensitive];
cdsDocs.Filter := 'Product LIKE ''%' + txtFilter.Text + '%''';
cdsDocs.Filtered := True;
finally
cdsDocs.EnableControls;
end;

并考虑该数据集的 Product 字段仅包含值“b”和“B”。

  1. 当 (txtFilter.Text = 'b') 时,我只得到 'b'(我期待 'b' 和 'B')
  2. 当 (txtFilter.Text = 'B') 时,我只得到 'B'(同样,我期待 'b' 和 'B')

LIKE '%b%' 和 foCaseInsensitive 似乎不能一起工作?我应该做什么才能让它发挥作用?我阅读了文档,但找不到我的错误(如果有的话)。 TIA。

最佳答案

一切都工作正常,直到我尝试扩展过滤以允许不区分大小写的搜索(我尝试使用现有的 FilterOption foCaseInsensitive),现在突然我有一个错误?不,这对我来说没有意义。我决定以另一种方式实现所需的不区分大小写的过滤,并保持我的 self 完整。

这是修改后的代码(完美运行)

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 数据库。如果连接到不同的数据库,您可能需要将“UPPER”函数替换为您选择的 RDBMS 的等效函数(无论您在等效 SQL WHERE 子句中使用什么)

关于Delphi TClientDataSet 使用 LIKE 和 foCaseInsensitive 进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17780969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com