【问题标题】:How to migrate from ADO Filtering code to Firedac如何从 ADO 过滤代码迁移到 Firedac
【发布时间】:2021-09-24 13:31:46
【问题描述】:

我有这个代码:

datamodule1.tbabonne.Filter := '';
  if (scGPEdit2.Text) = '' then exit ;
  try
    ref_Abonne:= QuotedStr (scGPEdit2.Text + '*');
    if (scGPEdit2.Text <> '') then
      datamodule1.tbabonne.Filter:= Format('(ref_Abonne LIKE %s)', [ref_abonne])
    else
      datamodule1.tbabonne.Filtered := Trim((scGPEdit2.Text)) <> '' ;
  except
    abort;
  end;
  //edit1.Text := '';
  end;

我的问题是:
上面的代码在 ADO 中用作魅力时不适用于 Firedac

【问题讨论】:

    标签: sql-server delphi ado firedac delphi-10.3-rio


    【解决方案1】:

    在 FireDAC 过滤器中,通配符是 _ 用于单个字符,% 用于多个字符 - 请参阅提供此示例的 http://docwiki.embarcadero.com/Libraries/Sydney/en/FireDAC.Comp.Client.TFDQuery.Filter

    在使用 LIKE 运算符时,您可以在条件中使用标准 SQL 通配符,例如百分比 (%) 和下划线 (_)。以下过滤条件检索所有以“F”开头的国家

    国家喜欢“F%”

    所以你需要调整你的线路

    ref_abonne:= QuotedStr (scGPEdit2.Text + '*');
    

    相应地,使用 LIKE 运算符和% 通配符。

    只是猜测,但也许 ADO 使用了* 通配符和= 运算符来隔离,例如VB 用户来自 SQL 通配符和语法。

    更新这是一个使用 FireDAC % 通配符和 LIKE 运算符的示例项目 在一个过滤器中。请注意内联 cmets。

      TForm1 = class(TForm)
        //  Create a new VCL project and drop the following components
        //  onto it.  There is no need to set any of their properties
        FDMemTable1: TFDMemTable;
        DataSource1: TDataSource;
        DBGrid1: TDBGrid;
        edFilter: TEdit;
        //  Use the Object Inspector to create the following event handlers
        //  and add the code shown in the implementation section to them
        procedure FormCreate(Sender: TObject);
        procedure edFilterChange(Sender: TObject);
      public
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.edFilterChange(Sender: TObject);
    begin
      UpdateFilter;
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      DBGrid1.DataSource := DataSource1;
      DataSource1.DataSet := FDMemTable1;
    
      //  Adjust the following line to suit the location of Employee.Fds on your system
      FDMemTable1.LoadFromFile('D:\D10Samples\Data\Employee.Fds');
    
      FDMemTable1.IndexFieldNames := 'LastName;FirstName';
      FDMemTable1.Open;
      FDMemTable1.First;
    
      //  Make the filter disregard string case
      FDMemTable1.FilterOptions := [foCaseInsensitive];
    
      UpdateFilter;
    end;
    
    procedure TForm1.UpdateFilter;
    var
      FilterExpr : String;
    begin
      FilterExpr := edFilter.Text;
      if FilterExpr <> '' then
        FilterExpr := 'LastName Like ' + QuotedStr(FilterExpr + '%');
      FDMemTable1.Filter := FilterExpr;
      FDMemTable1.Filtered := FDMemTable1.Filter <> '';
    end;
    

    然后编译运行

    【讨论】:

    • 请您向我解释一下我上面代码中通配符的正确用法,我仍然无法理解您在上面向我推荐的解决方案
    • 我在文章中引用的Country LIKE 'F%' 有什么不明白的地方。?顺便说一句,我认为您在 q 中编辑的更改是错误的 - 它仍然有 ref_Abonne:= QuotedStr (scGPEdit2.Text + '*') 行,您仍然错误地添加了 ADO * 通配符,
    • 谢谢@MartynA 先生的回复,对不起,我不懂英文很快,但是有了你的更新回复,我现在明白了你上面的答案,非常感谢,你的例子很有用跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多