madlan madlan - 8 months ago 65
SQL Question

Monitor SQL Server for new row

I'm creating an application that notifies users if a new row has been added to a table in the applications database. Would it be best to create a trigger for this or just check every few seconds directly from the application? Thoughts?


Use Query Notifications. This is the SQL Server feature that allow notifications to be actively pushed to the client when data was changed by other user. It is better known as SqlDependency. You subscribe a query and you get notified when the result set has changed (rows were added, deleted or modified).

Eg. using a LinqToCache query and reacting to a invalidation notification:

var query = (from r in ctx.table select r).AsCached(
 "table", new CachedQueryOptions() {
    OnInvalidated = (sender, args) {
      // the query was invalidated, a new row was added!
      // refresh display or notify user