关于AdoQuery中的Refresh函数

首先看看Delphi的官方文档中关于refresh的说明

Refetches data from the database to update a dataset抯 view of data.

procedure Refresh;

Description

Call Refresh to ensure that an application has the latest data from a database. For example, when an application turns off filtering for a dataset, it should immediately call Refresh to display all records in the dataset, not just those that used to meet the filter condition.

Note: The Refresh method does not work for all TDataSet descendants. In particular, TQuery components do not support the Refresh method if the query is not "live".To refresh a static TQuery, close and reopen the dataset.

TDataSet generates a BeforeRefresh event before refreshing the records and an AfterRefresh event afterwards.

Note: Most datasets try to maintain the current record position when you call refresh. However, this is not always possible. For example, the current record may have been deleted from the server by another user. Unidirectional datasets have no mechanism for locating the current record after a refresh, and always move back to the first record.

Warning: Unidirectional datasets refresh the data by closing and reopening the cursor. This can have unintended side effects if, for example, you have code in the OnClose or OnOpen event handlers.


注意我红色标出的部分,如果我英文没理解错的话,就是Refresh会close,Open数据集,然后就会触发OnClose OnOpen,甚至 BeforeClose,BeforeOpen事件。

就是这个东西把我搞死了。。。我Refresh一个数据集发现数据竟然改变了。排查了好久发现触发了写的BeforeOpen函数,其中将Filter清空了。

另外,注意的是Refresh的性能不好,会比较慢。Refresh会比较提取前后数据,然后进行改变。但是它刷新完后会重新定位到你原来的数据指针上。还有Refresh不会造成Grid闪动

在AdoQuery中还提供了Requery函数,同样可以刷新,就是数据指针会回到第一个上,它的效果等同于先Close,再Open。

比较高性能的模仿Refresh的刷新数据方式有:

方法一:用书签

var 
    SavePlace:   TBookmark; 
begin 
    SavePlace   :=   ADOQuery1.GetBookmark; 
    ADOQuery1.DisableControls; 
    ADOQuery1.Requery;     //与先Close再Open等效 
    ADOQuery1.GotoBookmark(SavePlace); 
    ADOQuery1.FreeBookmark(SavePlace); 
    ADOQuery1.EnableControls; //先定位再打开控件不会闪烁
end; 

    方法二、如果数据集有可唯一定位的关键字,如id,这样做: 
var 
    id:   Integer; 
begin 
    id   :=   ADOQuery1.FieldByName( 'id ').AsInteger; 
    ADOQuery1.DisableControls; 
    ADOQuery1.Requery; 
    ADOQuery1.Locate( 'id ',   id,   []);  
    ADOQuery1.EnableControls;   
end; 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值