简体   繁体   English

自动刷新TDataSet / DBGrid

[英]Auto refresh a TDataSet / DBGrid

I'm developing a software that displays information in a DBGrid via a TSimpleDataSet (dbExpress components) 我正在开发一个通过TSimpleDataSet (dbExpress组件)在DBGrid中显示信息的软件

The software in question is used on 2 different computers by 2 different people. 有问题的软件由2个不同的人在2台不同的计算机上使用。

They both view and edit the same information at different times. 他们都在不同的时间查看和编辑相同的信息。 I'm trying to figure out a way to automatically update the DBGrid (or rather, the DataSet , right?) on Computer B once Computer A makes a change to a row (edits something/whatever) and vice-versa. 我试图找出一种方法来自动更新DBGrid (或者更确切地说,该DataSet在计算机B,对吧?),一旦计算机A进行了更改的行(编辑东西/其他),反之亦然。

Currently I've set up a TButton named Refresh that once clicked executes the following code: 目前我已经设置了一个名为RefreshTButton ,一旦点击就会执行以下代码:

procedure TForm2.actRefreshDataExecute(Sender: TObject);

begin
    dbmodule.somenameDataSet.MergeChangeLog;
    dbmodule.somenameDataSet.ApplyUpdates(-1);
    dbmodule.somenameDataSet.Refresh;
    dbmodule.somename1DataSet.MergeChangeLog;
    dbmodule.somename1DataSet.ApplyUpdates(-1);
    dbmodule.somename1DataSet.Refresh;
    dbmodule.somename2DataSet.MergeChangeLog;
    dbmodule.somename2DataSet.ApplyUpdates(-1);
    dbmodule.somename2DataSet.Refresh;
    dbmodule.somename3DataSet.MergeChangeLog;
    dbmodule.somename3DataSet.ApplyUpdates(-1);
    dbmodule.somename3DataSet.Refresh;
end;

This is fine and works as intended, once clicked. 一旦点击,这很好并按预期工作。 I'd like an auto update feature for this, for example when Computer A edits information in a row, Computer B's DBGrid should update it's display accordingly, without the need to click the refresh button. 我想要一个自动更新功能,例如当计算机A连续编辑信息时,计算机B的DBGrid应该相应地更新它的显示,而不需要单击刷新按钮。

I figured I would use a TTimer and set it at a specific interval, on both software on both PC's. 我想我会使用TTimer并在两台PC上的两个软件上以特定间隔设置它。

My actual question is: 我的实际问题是:

Is there a better way than a TTimer for this? 有没有比TTimer更好的方法呢? If so, please elaborate. 如果是这样,请详细说明。 Also, if the TTimer route is the way to go any further info you might find useful to state would be appreciated (pro's and con's and so on) 此外,如果TTimer路线是进入任何进一步信息的方式,你可能会发现有用的状态将被赞赏(亲和和等等)

I'm using Rad Studio 10 Seattle and dbExpress components, the datasets connect to a MySQL database on my hosting where my website is. 我正在使用Rad Studio 10 Seattle和dbExpress组件,数据集连接到我的网站所在托管服务器上的MySQL数据库。

Thanks! 谢谢!

Well, Ken White and Sertac Akyuz are certainly correct that using a server-originated notification to determine when to refresh your local dataset is preferable to continually re-reading all the data you are using from the server. 好吧,Ken White和Sertac Akyuz肯定是正确的,使用服务器发起的通知来确定何时刷新本地数据集比从服务器不断重新读取您正在使用的所有数据更可取。

The problem AFAIK is that there is no Emba-supplied notification system which works with MySql. 问题AFAIK是没有Emba提供的通知系统,它可以与MySql一起使用。 See this list of databases supported by FireDAC's Database Alerts: 请参阅FireDAC数据库警报支持的此数据库列表:

http://docwiki.embarcadero.com/RADStudio/XE8/en/Database_Alerts_(FireDAC) http://docwiki.embarcadero.com/RADStudio/XE8/en/Database_Alerts_(FireDAC)

and note that it does not list MySql. 并注意它没有列出MySql。

Luckily, I think there is a work-around which should be viable for a v. small system like yours currently is. 幸运的是,我认为有一种解决办法对于像你这样的小型系统应该是可行的。 As I understand it, you and your colleague's PCs are on a LAN and the MySql Server is outside your LAN and on the internet. 据我了解,您和您同事的PC位于局域网上,而MySql Server位于局域网和互联网之外。 In that situation, it doesn't need a round trip to the server for one of you to get a notification that the other has changed something in the database. 在这种情况下,它不需要往返服务器,因为其中一个人可以获得另一个已经更改了数据库中某些内容的通知。 Using an analogy akin to Ken's, you can, as it were, lean over the desk and say to your colleague "Hey, I've changed something, so you need to refresh your data." 使用类似于肯的类比,你可以倚靠桌子,对你的同事说“嘿,我改变了一些东西,所以你需要更新你的数据。”

A very low-tech way of implementing that would be to have somewhere on your LAN a resource that both of you can easily get at, which you can update when you make a change to the DB that means that the other of you should update your data from the server. 一种非常低技术的实现方式是在局域网上的某个地方拥有一个你可以轻松获得的资源,当你对数据库进行更改时你可以更新,这意味着你们中的另一个应该更新你的来自服务器的数据。 One way to do that is to have a small, shared datafile with a number of records in it, one per server db table, which has some sort of timestamp or version-ID number which gets updated when you update the corresponding server table. 一种方法是使用一个包含大量记录的小型共享数据文件,每个服务器数据库表一个,它具有某种时间戳或版本ID号,在更新相应的服务器表时会更新。 Then, you can periodically check (poll) this datafile to see whether a given table has changed since you last checked; 然后,您可以定期检查(轮询)此数据文件,以查看自上次检查后给定表是否已更改; obviously, if it has, you then re-read the data you want from it from the server and update your local record of the info you read from the shared file. 显然,如果有,则从服务器重新读取您想要的数据,并更新您从共享文件中读取的信息的本地记录。

You can update the shared file using handlers for the events of your Delphi client-side datasets. 您可以使用处理程序为Delphi客户端数据集的事件更新共享文件。

There are a number of variations on this theme that I'm sure will be apparent to you; 这个主题有很多变化,我相信你会很明显; the implementational details really don't matter. 实施细节真的无关紧要。

To update the shared file I'm talking about, you will need to lock it while writing to it. 要更新我正在谈论的共享文件,您需要在写入时锁定它。 This answer: 这个答案:

How do I get the handle for locking a file in Delphi? 如何获取在Delphi中锁定文件的句柄?

will show you how to do that. 会告诉你如何做到这一点。

Of course, the shared local resource doesn't have to be a data file. 当然,共享本地资源不必是数据文件。 One alternative would be to use a Microsoft Message Queue service, which is sometimes used for this kind of thing, but has a steeper learning curve than a shared data file. 一种替代方法是使用Microsoft Message Queue服务,该服务有时用于此类事务,但具有比共享数据文件更陡峭的学习曲线。

By the way, this kind of thing is far easier to do (at least on a small scale like you have) if you use 3-tier database access (eg using datasnap). 顺便说一句,如果你使用3层数据库访问(例如使用datasnap),这种事情要容易得多(至少在你这样的小规模上)。 In a three tier system, only the middle tier (a Delphi datasnap server which you write, but it's not that hard) talks to the server, and the clients only talk to the middle tier. 在三层系统中,只有中间层(您编写的Delphi datasnap服务器,但并不难)与服务器通信,客户端只与中间层通信。 This makes it easy for the middle tier server to notify the other client(s) when one of them changes the db data. 这使得中间层服务器可以在其中一个客户端更改数据库数据时通知其他客户端。

The three-tier arrangement also helps minimise the security problems with accessing a database server via the internet, because you only need one secure connection to the server, not one per client. 三层安排还有助于最大限度地减少通过互联网访问数据库服务器时的安全问题,因为您只需要一个到服务器的安全连接,而不是每个客户端一个安全连接。 But that's straying a bit far from your immediate problem. 但这与你眼前的问题有点不同。

I hope all this is clear, if not, ask. 我希望这一切都很清楚,如果没有,请问。

Just use a timer and make it refresh the dataset every 5 min. 只需使用计时器并使其每5分钟刷新一次数据集。 No big deal. 没什么大不了。 If the usage is not frequent then you can set it to fire every 10 or 15 min. 如果使用不频繁,那么您可以将其设置为每10或15分钟触发一次。 There is nothing wrong with the timer if it set on longer intervals. 如果计时器间隔较长,则计时器没有任何问题。 Today's broadband connection's can easily handle the traffic so can Access. 今天的宽带连接可以轻松处理流量,因此可以访问。 If the table is not huge of course. 如果桌子当然不是很大。

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

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