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/
Related
I am using SignalR to update client-side when data is changing on the backend. The data can be either from SQL Server or NoSQL DB.
What I am doing now is: I have a timer on the backend that constantly is trying to check whether there is a change in the DB and if it detects it then I allow SignalR to update the client-side.
I find this approach not good: the timer's interval value can't be really small e.g. even with 1 second I have a huge load on the DB. Besides, it's not real-time and even 1 second is too big. Additionally, that it's quite difficult to detect a DB change if it's not about adding a new record, but calculating some hash for all the records every 1 second or even less is surely not an option.
I think I used to read about another approach that is based on some event triggered either by the DB or by something else, but I can't remember this.
So I was wondering if somebody could advise me of a better solution.
I'm afraid we need to take different actions for different scenarios on this case.
As you mentioned that your data may come from SQL server and nosql db. So I think the scenario should like:
You write code to update the data in both of the database, if so, you can write the calling signalr code after the updating data code, so that you are no need to using trigger.
Based on the scenario 1 as well, if the database you used support using stored procedure, you may also try to find the possibility of writing stored procedure to call the signalr code.
You may also check if the databases you used have event like "data_onchange_event", this document is related to this scenario. I mean if the data stored in the database updated, your database will send an event, you can then write custom code to capture these event and then call signalr.
If you can't or don't write code to update your databases, then you may only using external trigger to monitor the database, but just like you mentioned in the question, it doesn't support high performance because of huge load on the DB. It's based on the database but not the external trigger.
I need to try to notify a users who are modifying the same page that an update was made to an Excel grid SPA. I was thinking about passing pack and forth the date modified timestamp and if the original is in the past from the current in the database it would mean the grid was updated by someone else. Is there a better way to do this?
Since you've mentioned AJAX, I'll assume this is a web application. This sounds like an excellent candidate for bi-directional communication via websockets. I've used SignalR with great success. It will allow you to publish events from the server to any subscribed clients, allowing you to easily update what they are viewing.
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.
I am using MySQL with C# / Visual Studio 2008. I need to know any method so that if one user updates the database, all the logged in users over the network, are notified that a change has been occurred.
If i logged in the database through my application, at that time the serial no of the latest record in the database was 10, and then i someone with the privileges updates the record in the database and at the same time i am being notified that the database has been updated with a new record.
Common example can be of MSN messenger's alert for a friend when he gets online...
Though i really don't want that popup, but alert functionality is something like that...
One more thing to ask:
I want a functionality like if a record's status is not updated (for say about more than 3 hours), then i want all users to get alert about that issue also. This functionality is needed for all of the records, not specifically for new records.
Quite a long answer, but your best bet would be something like SqlCacheDependency.
Also you could use something called the "Reverse AJAX" technique, which is used by Google Mail notifications, and talked about here: How does incoming mail notification on Gmail works?
Besides those two, your own other options AFAIK is simple server polling.
Also, you haven't mentioned what your client is.
Is it a public web site on the internet? Is it an intranet app, it is a WPF app?
If it's a web site, you're best best is client-side callbacks using the Reverse AJAX technique i mentioned.
You probably need to design some kind of poll functionality in your client. For example, you can send a request each minute asking the database "Have you updated anything since [date the client latest got updates]?" If the database answers true, you let the client request the full set of updates. If not, you let it sleep another minute.
Using the SqlDependency Class is a better way to make all your data driven more faster and efficient. It just remove the need of constantly re-query your database checking every time when a changes is made in the data.
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.