SqlDependency issue with the asp.net

前端 未结 2 553
[愿得一人]
[愿得一人] 2021-01-18 10:43

I am trying get the changed values from the sqlserver using the server_broker feature.

my code is like below

 protected void Page_Load(object sender,         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-18 11:33

    All the code you've shown is ASP.Net side code. The grid is shown on the client, is an HTML element. you need to notify the client of the change. HTTP refresh is not a trivial issue, you have to either:

    • use a timer of the client and periodically poll for changes
    • use WebSockets
    • use Server-Sent Events (Content-Type: text/event-stream) but this is not supported by IE

    Polling works but it can get taxing on the server, specially with large number of clients. There are countless examples of ASP.Net backed polling using Ajax.

    WebSockets requires Windows 8/windows Server 2012 and IIS 8, see Support for WebSockets Protocol

    You should probably also look into SignalR, which is an ASP.Net library specifically developer for pushing updates to the client.

    As you can see, my answer does not even touch the topic of Query Notifications. Getting the notification from the DB to the ASP.Net middle tier is only one part of the equation, and SqlDependency is indeed the right answer. But you're completely missing the second part, the pushing of notification from mid-tier to the browser. You should only notify the browser that the update occurred and the client should refresh. Let the refresh load the data using the usual Page_load event. Use SqlCacheDependency to server the page, this will automatically cache results and refresh the cache on any update.

提交回复
热议问题