1

This may be a silly question ,but please help me sort this.

I have a SQL database with tables and whenever there is row added/deleted/updated ..(any operation) in the table a notification should be available through a desktop c# app running in system tray.

There is already notification mail sending mechanism in SQL SERVER but i dont want to get notified by mail.

Mail notification screen below :

enter image description here

Is there any better approach to get notification through app even if the database is modified via management studio?

I used to check the count of rows and will get notified if count is increased / decreased i would be notified about inserting / deleting of rows .

But that doesnt apply for update !

Edit 1 :

How to overcome if possibilities are available for arising limitation situations like this Stack Overflow post answer for SqlDependency ?

Community
  • 1
  • 1
Tharif
  • 13,794
  • 9
  • 55
  • 77
  • You get number of rows updated though? – Lloyd May 27 '15 at 09:37
  • Have you heard about Notification Services? – Oscar May 27 '15 at 09:37
  • That sounds horrible... but: in your scenario, what is the connected system that you want to be notified? – Marc Gravell May 27 '15 at 09:41
  • @MarcGravell thank you for your time..is there any work around for that situation ? How can we achieve that – Tharif May 27 '15 at 09:42
  • @utility that depends: *what is the connected system that you want to be notified*? What is the context here? What is the problem you are trying to solve? Personally, I would (and indeed: do) use pub/sub that is completely separate to SQL-Server and the database - i.e. if I'm doing something other code will want to know about: change the data *and* publish a message to a pub/sub channel. – Marc Gravell May 27 '15 at 09:43
  • a c# application is the connecting system here..Hearing SqlDependency first time..do that consume system resource for constantly checking the database?..@MarcGravell please do share some light about that pub/sub – Tharif May 27 '15 at 09:46

2 Answers2

1

Perhaps you're looking for SqlDependency .

And more specifically, the OnChange event.

(There's also the SqlCommand.Notification property)

ispiro
  • 26,556
  • 38
  • 136
  • 291
0

I made a small sample for you

create for each table a trigger for Inserting, Updating and deleting. Perhaps use a History table for each table , this way all your changes are recorded as well

Create a table as follows

Create Table Notifications
(
 Id bigint Identity(1,1) Not null,
 TableName nvarchar(250),
 ChangedOn DateTime,
 Notified bit
)

each time your trigger happens write to the notifications table the following values:

Insert Into Notifications
(TableName, ChangedOn, Notified)
Values
('YourTableName', GetDate(), 0)

Create an application with an easy timer that will periodically check the table values as follows:

ie: C#

private List<Notification> _notificationsToShow;

Private void DoNotify()
{
    var notifs = Notifications.Where(t=> !t.Notified).ToList();
    if(notifs.Count > 0)
    {
        _notificationsToShow.AddRange(notifs);            
        foreach(var Notification in notifs)
        {
            //Save to database as user is notified
            Notification.Notified = true;
            Notification.Save(); 
        }            
    }
    DoSomethingAlertish();
}

private void DoSomethingAlertish()
{  
    if(_notificationToShow.Count == 0 || _notificationToShow == null)
        return;

    var s = new StringBuilder();
    foreach(var v in _notificationToShow)
    {
        s.AppendLine(v.TableName);
    }
    alert(s.ToString());
}

//Create an event that will empty the list (user must do trigger this action)
private void UserNotifiedAction() 
{
    _notificationToShow = new List<Notification>();
    DoSomethingAlertish();
}

The downsize of this method is the following:

for each table triggers must be created. This means 3 triggers per table

so when you have a small application with for example 15 tables this already means the creation of 45 triggers, now do think of a situation with 1000 tables, you get the point.

Also, be aware that the Notifications table will grow like a maniac. So perhaps you need to clean it now and then.

the good part is then that after creating all your triggers and storing the values in the table, you can use whatever technology available to create a notification. You want it in the tray bar now, perhaps in the future you still want a mail, everything is already running and collecting all your values, the only part where you then have to think about is what technology to call it with.

I hope i provided you with a solution that can help you in the future. Again, the solution you ask can be simpler but it depends how future-aimed you are.

Schuere
  • 1,579
  • 19
  • 33