views:

125

answers:

3

i want to build a little app that pops up a tray notification (or a toast popup or something) whenever an update or insert going into a certain table in a SQL server database.

What is the simplest way of doing this as i want to avoid polling if possible?

+1  A: 

You could trigger extended stored procedure, which would notify your tray.

Maybe this link is better.

Unreason
+2  A: 

Extended stored procedures are what I thought of first, too, and are probably the solution I'd use if I wanted to run the monitoring app on the SQL Server itself. But I'm guessing that's probably not the case.

I'd suggest using MSMQ as an intermediate layer, myself, since it comes with just about every version of Windows these days and is more or less tailor-made for this sort of thing. So, going through the layers, here, you have:

  1. UPDATE and INSERT triggers on your certain table, which call...
  2. ...a .NET assembly (added using CLR integration), which...
  3. ...puts a message describing the insert/update into an MSMQ queue on the server, which...
  4. ...is received by your tray app, wherever it's running...
  5. ...and then displayed.

There's sample code for accessing MSMQ from SQL Server here: http://www.codeproject.com/KB/database/SqlMSMQ.aspx

Cerebrate
+1, but the article states that the best way to do it is using CRL, and that's available only since SQL Server 2005
Unreason
Fair comment, I should have mentioned that.To expand on that, then, you *can* also use this technique using an extended stored procedure to call MSMQ to queue your insert/update notification, and so still get the benefits if your monitoring app is to run on a remote machine. But, frankly, if you are on SQL 2005 or later and so can do it the CLR way, I'd strongly suggest that you do do it that way. It's much easier to implement. :)Thanks!
Cerebrate
+5  A: 

Query Notifications. This is the SQL Server feature that allows an application to subscribe to notifications pushed from the server when data is changed. It is usually leveraged through the SqlDependency class.

I have recently posted the LinqToCache project that allows you to add SqlDependency based notifications and cache invalidation to LINQ queries:

var query = (from r in ctx.table select r).AsCached(
 "table", new CachedQueryOptions() {
    OnInvalidated = (sender, args) {
      // the query was invalidated, data has changed
      // refresh display or notify user
    }
 });
Remus Rusanu