Datagrid 不会刷新更改的数据

发布于 2024-09-03 20:59:57 字数 94 浏览 3 评论 0原文

有没有办法让datagrid监听数据库并在数据库数据发生变化时自动更新数据?我使用 SQL Server 数据库。

如果可能的话我想使用 Linq-2-SQL

Is there any way to have a datagrid listen to the database and automatically update the data if the database data is changed? I use a SQL Server database.

I'd like to use Linq-2-SQL if possible

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聊慰 2024-09-10 20:59:58

因为@Slaggg问:有相当简单的方法可以做到这一点,但它们几乎肯定会涉及大量编码,这会相当显着地影响性能,而且我强烈怀疑这会比实际情况更麻烦值得。

也就是说,对于典型的 n 层应用程序,在非常高的级别上,您将需要:

(1) 一种在数据更改时通知中间层的方法。您可以在每个表中使用自定义代码触发器来触发某种通知(可能使用 WCF 和 CLR 存储过程),或者您可以使用 SqlDependency 对象。也许第二个效果会更好。

(2) 一种通知连接到该中间层的每个客户端的方法。假设您使用的是 WCF,则需要使用可用的双工绑定之一,例如 Net.TCP 或 HttpPollingDuplex(对于 Silverlight)。您需要确保在客户端和服务器上都正确配置。您还需要手动跟踪哪些客户端可能对更新感兴趣,以便您知道要更新哪些客户端,并且当它们消失或超时时,您需要能够将它们从列表中删除。来自 MS WCF 团队的 Tomek 在他的 博客

(3) 一旦从中间层收到某些内容已更改的通知,就会更新本地客户端的模型和/或视图模型和/或 UI 的机制。这比您想象的要复杂:在正常情况下保持 UI 与数据模型同步已经够困难的了,但是当数据模型也可以从另一个方向在您的控制下发生变化时,事情就会变得更加复杂。

此类通知背后的想法非常简单:但是正确掌握所有细节可能会让您一直调试到深夜。两年后必须支持这一切的人会咒骂你的名字。

希望这有帮助。

Because @Slaggg asked: there are fairly straightforward ways of doing this, but they're almost certainly going to involve a lot of coding, it'll impact performance pretty significantly, and my strong suspicion is that it'll be more trouble than it's worth.

That said, for a typical n-tier application, at a very high level you'll need:

(1) A way to notify the middle tier when the data changes. You could use custom-code triggers inside each the table that fire off some sort of notification (perhaps using WCF and CLR stored procedures), or you could use a SqlDependency object. Probably the second would work better.

(2) A way to notify each client connected to that middle tier. Assuming that you're using WCF, you'll need to use one of the duplex bindings that are available, such as Net.TCP or HttpPollingDuplex (for Silverlight). You'll need to make sure this is configured correctly on both the client and the server. You'll also need to manually keep track of which clients might be interested in the update, so that you can know which ones to update, and you'll need to be able to remove them from that list when they go away or timeout. Tomek, from the MS WCF team, has some pretty good examples on his blog that you might want to investigate.

(3) A mechanism to update the local client's model and/or viewmodel and/or UI once you get the notification from the middle tier that something has changed. This is more complicated than you'd think: it's difficult enough to keep your UI in sync with your data model under normal circumstances, but it gets dramatically more complicated when that data model can be changing under you from the other direction as well.

The idea behind these sorts of notifications is straightforward enough: but getting all the details right is likely to keep you debugging way into the night. And the guy who has to support all this two years from now will curse your name.

Hope this helps.

画骨成沙 2024-09-10 20:59:58

这取决于您更新数据库的位置:

  • 来自相同的上下文(在
    Silverlight,您是否要添加,
    删除,在同一页面上编辑)
  • 从您的子窗口
    Silverlight 应用程序
  • 来自
    外部的、不相关的工具、外部
    您的 Silverlight 应用程序的

It depends from where you are updating the database:

  • From the same context (in
    Silverlight, are you adding,
    deleting, editing on the same page)
  • From a ChildWindow in your
    Silverlight application
  • From an
    external, non-related tool, outside
    of your Silverlight application
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文