SQL Server Database Change Listener C# - c#

I want to listen for changes to data in a SQL Server database from C#. I was hoping that there would be some sort of listener which I could use to determine if data that I have is stale. Despite being a fairly common scenario I can't find any solutions which aren't to simply poll the database.
I use Linq-To-SQL to access the data and hence have a DataContext object, I was hoping I could listen for an on data changed event but I can't seem to find one.
I appreciate that it's a non-trivial barrier (From C# method to SQL Server DB), the reason I expected this to be a solved problem is that it's a common requirement for GUIs. If it's not possible to Listen for updates how to you keep the Data displayed in a GUI fresh (When it's backed by a SQL Server data source).
Although this isn't for GUI work I was expecting to adapt something from that realm.
Is there a way to subscribe to SQL Server database change events in C#?

I've never used them before, but have you tried SQL Server Events notifications?
See this article: Getting Started with SQL Server Event Notifications

You're looking for the SqlDependency class, which allows you to listen for changes to the resultset of a SQL query.

The DataContext won't offer you any type of listener functionality with SQL Server. Your best bet is to create a polling application, or even a separate thread which polls the database periodically for changes and expose an event which your main application can listen to.

If you are using SQL Server 2008, there is a built in Change Data Capture that's pretty handy.
http://msdn.microsoft.com/en-us/library/bb522489.aspx
You can read the CDC data.

I would use a table with a single row in the db to catalog last updated, inserted, or deleted events and then create triggers on each table of importance to update this table and then poll this table for changes.

Related

Does RowChanged event on trigers when i make change in databse?

I am learning databases with MySQL and C#. I am looking for way to respond to database changes so that users doesn't work with old data. Is the RowChanged event on DataTable object triggered only when the Row object is changed or does it respond to changes to database table.
The RowChanged event will only be raised for local changes to the Row; it will not be raised if there are changes on the database server. MySQL Server doesn't support the server-side features necessary to make this possible. (The SqlDependency class, which is used for Microsoft SQL Server, is based on the Service Broker infrastructure, which doesn't exist in MySQL.)
To get updates when the database is changed, you'll have to implement some sort of polling based on the shape of the data in your tables (e.g., does it have a TIMESTAMP column or some other value that always changes?); see Update C# client whenever database is updated.

Server Sent Events with SQL

I can't seem to find a way to do this, so I decided to ask here. I am making an asp.net site which uses data from a SQL Server database. I am using javascript to get the data and format it as I want.
The issue is that I want to use server sent events in order to get the new entry in my database and display it in the page of the site. So far the only examples I saw were with timers on the server side and on the period they send data to the javascript. But I can't seem to figure out how I should do it so that when a new row enters the database to fire the event.
That should be done on server side but I don't have a clue where to begin.
SqlDependency:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldependency.aspx
http://dotnet.dzone.com/articles/c-sqldependency-monitoring
Using the SqlDependency Class is a good way to make your data driven application (whether it be Web or Windows Forms) more efficient by removing the need to constantly re-query your database checking for data changes.
That’s how you use the SqlDependency Class for monitoring data changes in your database without having to use something like a timer control to re-query at certain intervals.
Here is an old book mark:
http://rusanu.com/2006/06/17/the-mysterious-notification/

Best way to push change data from sql server to windows/web application

i have win apps which will load all data initially from and display through grid but from then next when any data will change in db or any data will be inserted newly in db then only change or newly inserted data need to be pushed from d site to my win apps. now only sql dependency class is coming to my mind but there is a problem regarding sql dependency class that it notify client but do not say which data is updated or inserted.
so i am looking for best guidance and easy way to achieve my task. what will be the best way to push data from sql server to win or web client.
Will change tracking work for you? SQL Server Change Tracking
Consider using SQLDependency. In a nutshell: you set up a query, and get notified whenever the results of that query change.
I believe this just notifies you of the change--you then need to query the data to get the actual differences.

Notification from the db instead of polling. Is it possible?

Hy guys,
to monitor a specified table on db I'm polling this one every few milliseconds (10-20 ms).
Is there any chance to have a notification (avoiding SqlDependency, that in my scenario is too slow) instead polling?
Do you have any idea?
My scenario is .net + Sql Server 2008
Thanks!
It can be done, using a CLR stored procedure that will call a WCF/or a webservice. It is not something very difficult to do.
This needs practically 2 steps.
The modification of data.
After you modified the data you have to send the data to the clr stored procedure. The easiest way is to write it into one ore more temporary tables.
The clr stored procedure.
The clr store procedure will connect to the db with
"context connection=true"
so that you will have access to the stored procedures that you need. After loading the data you send it to a server (WCF/webservice). In the CLR you just need to add the service references that you need. Also on the server you will have to register some dlls for the server to use:
system.web
smdiagnostics
system.runtime.serialization
system.identitymodel
system.identitymodel.selectors
system.messagng
system.transactions.bridge
system.servicemodel
Everything else is plain .NET code to call a WCF/Web service.
This approach is very fast and very reliable.
To get onChange event notification in SQL, you can use the Query Notification feature which is built on Server Broker.
Alternatives are:
DB trigger
SignalR
Change Data Capture
Change Tracking
You could use the SqlChangeMonitor class, but that wraps SqlDependency in cached data scenarios. Your question is a little vague on why you want to do this, though.
I see you say you need notifications (and SqlDependency is available for that), but maybe you don't need instant notifications, and efficiently reading the changes periodically will do the task. If so, go google Change Data Capture, and Change Tracking.
Can the calling application be changed to say write to a queue instead?
If not I guess a trigger on the database which calls a CLR Stored Procedure? That could fire off any kind of event required.

SQL Server Notifications

I'm building an application which extracts data from SQL Server, the version of the server might differ from 2005 to 2012 but most important are 2008 and 2012.
If I want to listen to changes to certain tables and rows in the database, and send or fetch these changes and use them in my C# application what is the best approach?
I will not need to update any data in the database, that is the job for another already existing application.
I just want to listen to changes, or poll for changes, or anything like that.
In your experience, what is best, Service Broker? Query notifications? MSMQ? SqlDependency? Change data tracking?
Thanks in advance!
Consider using SQL Server CLR integration in combination with triggers.
For example you can protocol the changes you wish (let say in a table you create for this purpose), and you can run a watch dog process which pulls the data in constant intervals or at certain points of time up to your needs.
I think you can make use SQL Server exposed performance counters.

Categories