Delphi 关于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的刷新数据方式有:


————————————————
版权声明:本文为CSDN博主「路上的猫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jdl2011/article/details/7872968