There is also an ADO.NET SqlDependency mechanism if you use ADO.NET on the client side with C # or VB.NET
The SqlDependency object may be associated with the SqlCommand in order to determine when the query results differ from those originally received. You can also designate a delegate for the OnChange event, which fires when the results change for the associated command. You must associate SqlDependency with the command before you execute the command. The HasChanges SqlDependency property can also be used to determine if query results have changed since the data was first received.
Basically, you bind SqlDependency to your SqlCommand and provide an event handler that is called when the values ββthat make up the result set of this SqlDependency change.
using(SqlCommand cmd = new SqlCommand(queryStatement, _conn)) { cmd.Notification = null; SqlDependency dependency = new SqlDependency(cmd); dependency.OnChange += new OnChangeEventHandler(OnChange); ...... }
In the event handler, you can do what you need.
void OnChange(object sender, SqlNotificationEventArgs e) { SqlDependency dependency = sender as SqlDependency; (do whatever you need to do - eg reload the data) }
Mark
source share