Click here to Skip to main content
13,358,079 members (56,806 online)
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 24 Mar 2007

Advanced SQL 2005 SQLNotificationRequest Functionality with C#

, 24 Mar 2007
Rate this:
Please Sign up or sign in to vote.
Using the SQLNotificationRequest object and the Service Broker to get row and column change notifications, mimicking supposed SQLDependency capabilities


SQL Server 2005 and ADO.NET 2.0 promised a powerful notification of database changes through the SQLDependency object. While SQL 2000 could only inform a client app of changes at the Table level, SQL 2005 promised the ability to notify of changes at the query level. If changes to the database table affect a query given to SQL 2005, a notification is given to the client app. If data is changed that does not affect a query given to SQL 2005, then no notification takes place.

Anyone that has tried using the SQLDependency object has found that this functionality does not actually exist. Notifications are still only at the table level, no matter what query is given to SQL 2005 to examine. I present here a workaround using SQL 2005 triggers and TSQL Service Broker Conversation to send a notification to a client app.

Using the Code

Just unzip the attached code, run the SQL script, and then run the C# application. Make changes to the SQLNotificationRequestTable (update, insert, delete), and see your changes be reported on the C# form...

The C# portion of this article is taken from this MSDN page with some slight modifications and one large modification: There is a location in the code where you can handle the Notification as an SQLDataReader and examine the message. I use this functionality to get the messages and display them.

The SQL portion of this article I wrote myself once I learned how the Service Broker and TSQL Triggers worked. The Service Broker is a complex concept, and I was thrilled to actually get my messages working. One other avenue I explored was the Microsoft.SQLServer.Samples namespace, which has some C# classes for the Service Broker, but I actually hit a dead-end using these classes. There may be a better way to do this if you are super knowledgeable in the Service Broker.

The majority of the SQL is devoted to examining the inserted and deleted tables to determine what caused the trigger to fire. The contents of the inserted or deleted tables are copied to a string variable which is then inserted into an XML variable because this Service Broker Message requires well formed XML. Then, in the last part of the SQL, I send the message.

Points of Interest

I have never tried this, but according to documentation Client and Database can be separate computers, and the IP address of the client app would replace 'LocalHost' in the SQL script attached when creating a Service Broker Route. Would love to hear back if anyone tries that.

I also do not know how production quality this is, and I haven't tried this on multiple tables.

I tried to copy snippets of my code into this article, but the formatting would not behave, so refer to the download.


  • 24th March, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Todd Wilder
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Member 1079835224-Nov-14 22:05
memberMember 1079835224-Nov-14 22:05 
QuestionMultiple Clients Pin
Bendage27-Oct-11 6:57
memberBendage27-Oct-11 6:57 
AnswerRe: Multiple Clients Pin
Todd Wilder27-Oct-11 9:41
memberTodd Wilder27-Oct-11 9:41 
GeneralRe: Multiple Clients Pin
aliahmadi13679-Mar-13 3:35
memberaliahmadi13679-Mar-13 3:35 
QuestionDeployment Pin
Humdy11-Jul-11 1:50
memberHumdy11-Jul-11 1:50 
QuestionDetermine change of a special column Pin
beni27-Jan-09 3:37
memberbeni27-Jan-09 3:37 
AnswerRe: Determine change of a special column Pin
Todd Wilder10-Feb-09 17:34
memberTodd Wilder10-Feb-09 17:34 
GeneralMy vote of 1 Pin
chenghezhang6-Dec-08 23:16
memberchenghezhang6-Dec-08 23:16 
GeneralRe: My vote of 1 Pin
Todd Wilder10-Feb-09 17:29
memberTodd Wilder10-Feb-09 17:29 
QuestionReplace Trigger Pin
dipak_narsinghani29-Apr-08 1:17
memberdipak_narsinghani29-Apr-08 1:17 
Can we replace trigger with any other code?
If yes, please tell how?
GeneralTimeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Pin
vineet1378019-Apr-08 0:15
membervineet1378019-Apr-08 0:15 
Generala code was not understanded Pin
jason_mf5-Mar-08 16:33
memberjason_mf5-Mar-08 16:33 
GeneralSQLNotificationRequest Pin
sssergei31-May-07 3:03
membersssergei31-May-07 3:03 
GeneralRe: SQLNotificationRequest Pin
Todd Wilder31-May-07 21:17
memberTodd Wilder31-May-07 21:17 
GeneralRe: SQLNotificationRequest Pin
sssergei4-Jun-07 6:28
membersssergei4-Jun-07 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 24 Mar 2007
Article Copyright 2007 by Todd Wilder
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid