如何在TADOQuery中使用RecordSet.Find? [英] How to use a RecordSet.Find with TADOQuery?

查看:168
本文介绍了如何在TADOQuery中使用RecordSet.Find?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此问题中



Delphi ADO:在错误上使用数据集过滤器定位



描述了一个ADO错误,其中在.Locates期间忽略了过滤器字符串。 / p>

这会给我们从BDE迁移带来麻烦,因为我们有很多代码会根据用户输入更改过滤器。



我们希望TADOQuery提供一个可行的迁移路径。我们错了。



我们当然可以将当前的过滤器更改为WHERE语句,但这需要大量工作,并且有可能将过滤器字符串连接到无过滤器的WHERE语句中,



上面链接的问题的公认答案表明可以使用 TCustomADODataSet.Recordset.Find



我们可以安全地使用RecordSet吗?在TADOQuery中查找仅用于实现.Locates?即RecordSet.Find是否更新TADOQuery封装在TADOQuery周围的内容?



如果是这样,有人可以显示从Delphi XE5到RecordSet的示例调用吗?我在弄清楚参数时遇到麻烦。

解决方案

您最好的选择是查看ADODB.pas的源代码 TCustomADODataSet.LocateRecord 。您将看到 Locate 的实现方式。



对于单个条件,它使用RecordSet 查找 方法。对于多个条件,它使用 过滤 属性。



使用查找很简单:

 使用...,ADODB,ADOInt; 

过程TForm1.Button1Click(Sender:TObject);
var
bm:TBookmarkStr;
开始
bm:= ADODataSet1.Bookmark;
//用于部分条件,例如ItemName就像 He *
ADODataSet1.Recordset.Find('ItemName ='Hello'',0,adSearchForward,adBookmarkFirst);
如果ADODataSet1.Recordset.EOF然后//未找到
ADODataSet1.Bookmark:= bm //恢复书签
否则
ADODataSet1.Resync([rmExact,rmCenter]); //设置活动记录并触发数据集更改事件
end;

由于 TADOQuery TCustomADODataSet 后代使用 RecordSet TADODataSet

一样没有问题

In this question:

Delphi ADO : Locate with dataset filter on bug

an ADO bug was described where the Filter string is ignored during .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.

We had expected TADOQuery to provide a working migration path. 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.

The accepted answer to the question linked to above suggests the possibility of using TCustomADODataSet.Recordset.Find

Can we safely use RecordSet.Find in a TADOQuery just to implement .Locates? i.e. Does RecordSet.Find update whatever wrapper TADOQuery puts around TADOQuery?

If so, can someone show a sample call from Delphi XE5 to RecordSet find? I'm having trouble figuring out the arguments.

解决方案

Your best option is to look at the source code of ADODB.pas TCustomADODataSet.LocateRecord. You will see how Locate is implemented.

For a single condition it uses RecordSet Find method. for multiple conditions it uses the Filter property.

Using Find is fairly easy:

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

这篇关于如何在TADOQuery中使用RecordSet.Find?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆