如何从 ADO 过滤代码迁移到 Firedac

How to migrate from ADO Filtering code to Firedac

我有这个代码:

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

在 FireDAC 过滤器中,单个字符的通配符是 _,多个字符的通配符是 % - 请参阅 http://docwiki.embarcadero.com/Libraries/Sydney/en/FireDAC.Comp.Client.TFDQuery.Filter,其中给出了这个示例

You can use standard SQL wildcards such as percent (%) and underscore (_) in the condition when you use the LIKE operator. The following filter condition retrieves all Countries beginning with 'F'

Country LIKE 'F%'

所以你需要调整你的线路

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

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

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

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

  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;

然后编译然后 运行