如何在 C# WinForms 中收到外部 MS Access 数据库更新的通知?
我有一个DataGridView
,它显示来自 MS Access 数据库的数据。我使用 DataSet
与 TableAdapter
和 BindingSource
将数据链接到 DataGridView
:
tableAdapter = new AccountsTableAdapter();
dataTable = new Accounts.AccountsDataTable();
tableAdapter.Fill(dataTable);
tableBindingSource = new BindingSource();
tableBindingSource.DataSource = dataTable;
dataGridView1.DataSource = tableBindingSource;
我想要了解当数据库表从应用程序外部修改时如何检测或收到通知 - 从 Access 界面或其他应用程序对数据库执行行更新、插入、删除。
另外,根据此假定的通知,我如何更新我的数据集,以便仅更新受影响的行 ->仅接收新插入的行、修改的受影响的字段值以及删除的索引。
因此,基本上,我想要获得的是一种将数据库表与 DataGridView 同步的方法。我已经设法将在 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.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道的唯一方法是轮询数据库。如果数据具有 LastModified 字段,您可以向数据库发出请求以获取更新的行,然后将结果合并到 DataSet 中。
例如,假设您通过 sql“SELECT * FROM Contact”填充表单。然后每隔一分钟左右运行一次查询“SELECT * FROM Contact WHERE LastModified > @LastFetched”,其中 @LastFetched 是您上次获取更新的时间。您需要从数据库中获取 @LastFetched 的值,因为客户端计算机和数据库服务器的时间同步可能不够接近,无法正常工作。
然后您只需要使用更新来更新数据集。如果表单绑定正确,它应该会自动更新。
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.
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.