简体   繁体   English

如何在TADOQuery中使用RecordSet.Find?

[英]How to use a RecordSet.Find with TADOQuery?

In this question: 在这个问题上:

Delphi ADO : Locate with dataset filter on bug Delphi ADO:在错误上使用数据集过滤器定位

an ADO bug was described where the Filter string is ignored during .Locates. 描述了一个ADO错误,其中在.Locates期间忽略了筛选字符串。

This is causing problems for us migrating from the BDE because we have a lot of code that changes the filter according to user input. 这给我们从BDE迁移带来了问题,因为我们有很多代码会根据用户输入更改过滤器。

We had expected TADOQuery to provide a working migration path. 我们期望TADOQuery提供一个可行的迁移路径。 We were wrong. 我们错了。

We can of course change our current filters to WHERE statements, but that's a lot of work and risk concatenating the filter strings to filter-less WHERE statements, etc. 当然,我们可以将当前的过滤器更改为WHERE语句,但这需要大量工作,并且存在将过滤器字符串连接到无过滤器的WHERE语句等的风险。

The accepted answer to the question linked to above suggests the possibility of using TCustomADODataSet.Recordset.Find 上面链接的问题的公认答案表明可以使用TCustomADODataSet.Recordset.Find

Can we safely use RecordSet.Find in a TADOQuery just to implement .Locates? 我们可以安全地在TADOQuery中使用RecordSet.Find来实现.Locates吗? ie Does RecordSet.Find update whatever wrapper TADOQuery puts around TADOQuery? 即RecordSet.Find是否更新TADOQuery封装在TADOQuery周围的内容?

If so, can someone show a sample call from Delphi XE5 to RecordSet find? 如果是这样,有人可以显示从Delphi XE5到RecordSet的示例调用吗? I'm having trouble figuring out the arguments. 我很难弄清楚这些论点。

Your best option is to look at the source code of ADODB.pas TCustomADODataSet.LocateRecord . 最好的选择是看ADODB.pas的源代码TCustomADODataSet.LocateRecord You will see how Locate is implemented. 您将看到如何实现Locate

For a single condition it uses RecordSet Find method. 对于单个条件,它使用RecordSet Find方法。 for multiple conditions it uses the Filter property. 对于多个条件,它使用Filter属性。

Using Find is fairly easy: 使用Find非常简单:

uses ..., ADODB, ADOInt;

procedure TForm1.Button1Click(Sender: TObject);
var
  bm: TBookmarkStr;
begin
  bm := ADODataSet1.Bookmark;
  // for partial condition use e.g. ItemName LIKE 'He*'
  ADODataSet1.Recordset.Find('ItemName = ''Hello''', 0, adSearchForward, adBookmarkFirst);
  if ADODataSet1.Recordset.EOF then // not found
    ADODataSet1.Bookmark := bm // restore bookmark
  else
    ADODataSet1.Resync([rmExact, rmCenter]); // set active record and trigger dataset change event
end;

Since TADOQuery is a TCustomADODataSet descendant there is no problem using RecordSet same as TADODataSet 由于TADOQueryTCustomADODataSet后代,因此使用与TADODataSet相同的RecordSet不会出现问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM