我如何在C#中的WinForms的外部MS Access数据库的更新得到通知? [英] How can I get notified in C# WinForms of external MS Access database updates?

查看:98
本文介绍了我如何在C#中的WinForms的外部MS Access数据库的更新得到通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的DataGridView 从一个MS Access数据库中显示的数据。我使用的是数据集的TableAdapter 的BindingSource 将数据链接到的DataGridView

I have a DataGridView that displays data from a MS Access database. I'm using a DataSet with a TableAdapter and a BindingSource to link the data to the DataGridView:

        tableAdapter = new AccountsTableAdapter();

        dataTable = new Accounts.AccountsDataTable();
        tableAdapter.Fill(dataTable);

        tableBindingSource = new BindingSource();
        tableBindingSource.DataSource = dataTable;

        dataGridView1.DataSource = tableBindingSource;

我想知道我怎么能检测或得到通知时,数据库中的表被从外面我的应用程序修改 - 行更新,插入,删除数据库从访问接口或从不同的应用程序执行

I want to know how can I detect or get notified when the database table gets modified from outside my application - row updates, inserts, deletes performed on the database from the Access interface or from a different app.

此外,在此presumed通知,怎样才能更新我的数据集,这样只有受影响的行应该被更新 - >只接收新插入的行的修改那些受影响的字段值和的指标删除。

Also, upon this presumed notification, how can I update my DataSet so that only the affected rows should be updated -> receive only the newly inserted rows, the affected field values of the modified ones and the indexes of the deleted.

所以,基本上,我想要获得与的DataGridView 同步我的数据库表的方式。我已经成功地保存到数据库中,我修改或插入行中的的DataGridView ,现在这将是很好的能够执行该数据库的倒数方 - 视图结合

So, basically, what I'm trying to obtain is a way of synchronizing my database table with the DataGridView. I've already managed to save to the database the rows that I modify or insert in the DataGridView, now it would be nice to be able to perform the reciprocal side of this database - view binding.

推荐答案

我知道的唯一方式就是轮询数据库。如果数据有上次更改时间字段,可以使数据库请求获得更新的行,然后将结果合并到数据集。

The only way I know of is to poll the database. If the data has a LastModified field, you can make requests to the database to get updated rows and then merge the results into your DataSet.

例如,假设您填充从SQL形式为SELECT * FROM联系。然后每分钟左右,运行查询SELECT * FROM联系WHERE上次更改时间> @LastFetched,其中@LastFetched是你最后得到了更新的时间。你会想从数据库中获取@LastFetched的价值,因为客户机和数据库服务器可能没有自己的时间sync'ed足够接近正常工作。

For example, let's say you populate the form from the sql "SELECT * FROM Contact". Then every minute or so, run the query "SELECT * FROM Contact WHERE LastModified > @LastFetched" where @LastFetched is the time you last got updates. You'll want to get the value for @LastFetched from the database since the client machine and database server might not have their times sync'ed close enough to work properly.

然后你只需要更新与更新的数据集。如果窗体正确绑定,它应该被自动更新。

Then you just need to update the DataSet with the updates. If the form is bound properly, it should automatically be updated.

这篇关于我如何在C#中的WinForms的外部MS Access数据库的更新得到通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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