windows application to Detect changes made to a database c# - c#

I have 2 applications both containing Datagridview connected to the same data-source.
When I insert data to the database via application1, the new data is instantly shown in the application1's Datagridview, I want the same new record to be shown in application2's Datagridview at the same time.
How do I achive this?

You can do this using Service Broker via the SqlDependency object. It will fire an event in your application when the results of your query change.

Related

Get notified when a cell value is changed in SQL Server database [duplicate]

This question already has answers here:
How to monitor SQL Server table changes by using c#?
(11 answers)
Closed 6 years ago.
I want to get notified when a certain change occurs in Database table. Consider the case: I want to perform a certain action when the column in a row changes its value to 5. How can I achieve it. I am using C# and entity framework to access the database.
For this you have to make a schedule job which will continuously(like interval of 5 minutes) ping database and notify you as like Facebook's notification bar.
Also you can write trigger on that table which will insert/update notification table and from there you will get notify.
The short answer is that you should probably try and manage this outside of SQL server. I have to assume that you have some application logic executing outside of SQL server that is the source of the update. Ideally your notification logic should be placed in your application tier before or after the database is updated.
Should you not be able to achieve this, three other options I can offer are:
polling You build a service that reads the value from SQL server in a loop. The loop should read the value periodically, and perform the notification. Most engineers avoid polling as from a best practices standpoint it is typically contra indicated due to adding persistent load to the database. Although polling should be avoided, it's surprisingly common in the field.
msmq You update the value via a stored procedure, and use this article to send a message to MSMQ when the value is 5. You will need to write a service to consume the MSMQ message and process the notification. You may use a WCF service using MSMQ transport to make this easy.
email You send an email using sp_send_dbmail in the update stored procedure, and build the necessary notification consumer(s). It should be noted that this method will likely also involve polling if you consume the email electronically. You can avoid that by using IMAP IDLE to process the email notifications. Try MailKit
Reporting services also apparently offers notifications, but I am not familiar with them.
using(var context = new FooEntities)
{
try
{
var customer = context.Customers.First(i=> i.CustomerID = 23);
customer.Name = "Bar";
context.SaveChanges();
//Write your notification code here
}
catch(Exception ex)
{
//Write notification along with the error you want to display.
}
}
Search in google there's many different way of displaying a notification.

SQL Server: alternative to polling -- call the second post-process storedproc

I'm coding a M2M data capture system using SQL Server 2012 and .net 4.5, the scenario is:
I have a remote data capture app, a web service, a DB.
The app captures data and invoke the web service to upload the data to the DB.
The web service call a "insert" storedproc to write raw data directly in Table A; and then, the web service returns a value telling that the insert was successful or not.
Now, a post-process storedproc needs to be run after the insert process to update another table (Table B).
Previously I used 'job agent' but since the required polling interval changed to 'less than 5 minutes', for the efficiency and real-time reason, I want to avoid to use the 'polling'.
Ideally, I want the app to be able to call the web service and get the return message/value, after that, the DB fires a stored proc to do the post-process work; the work may take longer so the app doesn't need to wait all the processes are done.
Can I fire the post-process sp from DB side? since the DB knows when the insert is done, and it saves communications from outside the DB.
Any suggestions?
You might think of using trigger plus Service Broker. In this way, the trigger will send a message to a queue. service broker will be fired to process the message. It decouples your table A update and table B update. If only use trigger to call table B, it will hold your table A update until the table B update finished.

Notify data was inserted into the table

Is there any way to notify working console application (call certain method) in case of any rows were added in the SQL Server 2012 DB table from anywhere?
1) One solution is to use Query Notification.
Quote:
Built upon the Service Broker infrastructure, query notifications
allow applications to be notified when data has changed.
http://msdn.microsoft.com/en-us/library/t9x04ed2(v=vs.110).aspx
https://www.simple-talk.com/sql/t-sql-programming/using-and-monitoring-sql-2005-query-notification/
2) Another solution could be
a simple AFTER INSERT/UPDATE/DELETE trigger that call sp_trace_generateevent
plus an extended events session which intercept user_event event (see here)
plus XEvent API > Microsoft.SqlServer.XEvent.Linq.dll, QueryableXEventData (see demo)

Watch a sql table continuously for a particular value

i was trying to connect a local machine with my web application.i need to get some data from local machine and insert it into my online table.User clicks a button and a new column is added to a online table named request which has status "new" in the beginning.i went for sql dependency which i installed as a windows service in local machine and it check the request table for any new insert.the service fetches some data from the local machine and posts it to the online database and change the status field in request table to "updated".
Now my problem is how can i notify my website that required data has been inserted in the table?? i need to watch the request table continuously until the value in the status column changes to updated.can i go for thread or timer ??
You can write an INSERT trigger in the live database which your website is using.
Use another Table RequestUpdates and write an INSERT trigger that adds a value to the table RequestUpdates in the live database.
Design your website to load the RequestUpdates table.
And when the user loggs in and Requests have been viewed, change the status in the live table to viewed.
In this way new requests will be shown that are not viewed the next time.

Auto updates OPC server

I have a C# source code for OPC server.
The code is working and I connect successfully but I have one big problem,
the data auto update does not work, in other words, I need to restart the server so it will
take the new data from the data base.
The code has an attribute named "updatePiriod", initialaized to 60000 ms and still, no auto updates.
There's more to getting data updates than simply connecting to the server. Have you 1) Created a Group; 2) Created items; 3) Activated the items; 4) Subscribed to callbacks?

Categories

Resources