Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tray notification on database update

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?

like image 831
leora Avatar asked Aug 03 '10 13:08

leora


People also ask

How do I get notifications of changes in the database?

The notifications are provided by Service Broker in your database, so will need to enable this service in your databse. The OnChange event will raise when specified table changes (update, delete, insert..) void Initialization () { // Create a dependency connection.

How to get a notify when some record is updated?

To get a notify when some record is updated, avoid the application to query the table you cab use TableDependency component (in your specific case SqlTableDependency ). Here is an example: The event handler is triggered for every INSERT UPDATE or DELETE operation done on the table, reporting you the modified value.

How to get a notification when a row changes in PostgreSQL?

For PostgreSQL I know a way to get notification from the database when a row changes. use trigger when insert / update / delete occurs. when event occurs send a notify to a client socket. be sure you app have a client to the server. then your app will receive a notification.

Is it possible to push database updates to the application?

There isn't any means of the database pushing notifications to the application. The application needs to poll the database to check for updates, and then deal with the updates appropriately. Show activity on this post. If by "updates to the database" you mean any update by any application, you're out of luck: it's not doable.


2 Answers

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
    }
 });
like image 108
Remus Rusanu Avatar answered Sep 28 '22 02:09

Remus Rusanu


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

like image 43
Cerebrate Avatar answered Sep 28 '22 02:09

Cerebrate