SqlDependency issue with the asp.net

前端 未结 2 552
[愿得一人]
[愿得一人] 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:23

    For an ASP.NET application you have to use the SqlCacheDependency class in name space System.Web.Caching; so you don't need the OnChange handler in this scenario.

    This article explains how to implement SqlCacheDependency for an asp.net client:

    Query Notification using SqlDependency and SqlCacheDependency

    However, you have mentioned you are using SQL Server Express 2012 and according to page Features Supported by the Editions of SQL Server 2012 and similar case in older SQl Server Express edition Using Service Broker with Sql Server Express 2008, since the SQL Server 2012 Express does not support the broker service, you will be unable to perform Query Notification in SQL Server 2012 Express.

    Edit: this paragraph is incorrect (reckon due to the MSDN page on which is based being misleading). Express editions support Service Broker and Query Notifications just fine, on all versions since SQL Server 2005. The 'Client Only' comment is better explained on the Express SQL Server Express Features page:

    SQL Server Express supports Service Broker, but direct communication between two SQL Server Express servers is not supported.

    this restrictions does not affect Query Notifications.

    These references are now merely informational Detecting Changes with SqlDependency , Creating a Query Notification , SqlDependency in an ASP.NET Application.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题