来自数据库的WPF实时更新 [英] WPF live Update from database
问题描述
我正在构建数据库SQL Server 2008的多用户WPF应用程序(要求是桌面应用程序).
I am building a multiuser WPF application (requirement is a desktop app), database SQL Server 2008.
有两种类型的用户.
第一类用户将输入记录,该记录将存储在表中.
The first type user will enter a record which would be stored in a table.
最初将为第二种类型的用户提供一个列表框,其中包含表中的记录.要求是必须实时更新列表框(即,如果第一个用户输入了新记录,则必须更新列表框).
The second type user initially will be presented with a listbox with the records from the table. The requirement is that the listbox must be updated live (i.e. if a new record is entered by the first user the listbox must be updated).
当前,我尚未实现数据库功能. 我目前正在使用ObservableCollection并模拟这种情况.
Currently I have not yet implemented the Database functionality. I am currently working with ObservableCollection and simulating this scenario.
我想知道实现这一目标的最佳方法. 我应该使用计时器并继续查询表吗?有没有更有效的方法? 甚至有可能(我应该切换到ASP.NET吗?)?
I would like to know the best approach for achieving this. Should i use a timer and keep querying the table? Is there a more efficient way? Is this even possible (should I switch to ASP.NET?) ?
如果您有任何建议和技巧,我将不胜感激.
I would appreciate any suggestions and tips that you may have.
推荐答案
尝试使用 SqlDependency ,因为当查询结果在服务器上发生更改时(由于来自另一个用户或进程的INSERT或UPDATE的结果,它可以在您的代码中引发事件(OnChange),例如)
Try using SqlDependency, since it can raise an event (OnChange) in your code when the results of a query would change on the server (as the result of an INSERT or UPDATE from another user or process, for example)
在CodeProject上将其用于简单的实时聊天应用程序的示例:
An example of its use on CodeProject for a simple live chat application:
http://www.codeproject.com/KB/database/chatter.aspx
这篇关于来自数据库的WPF实时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!