Notify data was inserted into the table - c#

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)

Related

How to send email notification using azure to user group when there are records in a table

.I am using azure sql database with my app hosted in azure
I tried using a Automation account, created a runbook but doesnt fetch any results
FullyQualifiedErrorId : CommandNotFound Exception.
CategoryInfo :Object Not Found: (SqlAutomation/RB_Record_AzureScheduler) Command Not Found exception
You can use the "when an item is created" and the "when an item is modified" triggers for SQL in Azure Logic App to react to data changes.
You can add condition and choose an action like sending an email.
The SQL connector in Azure Logic Apps uses a polling mechanism to query a table for changes using a TIMESTAMP / ROWVERSION column. This data type is specifically designed for this kind of processing in SQL. The polling query essentially selects all rows where the rowversion is greater than the last polled value. The behavior is reliable since the column is controlled by SQL Server and the performance is extremely fast in the case where there is no new data. When there is new data, the performance is comparable to a simple row query.
For more information, please read this article and this one too.

Web Service checking that DB is available

I need to create an ASP.NET web service that simply returns an information telling if a database is available or down when it's consumed.
So I would like to know if I can set a task that is executed inside the web service method on a regular basis to check the connection to the database and return the result via a URL.
No you cannot. Well, you could, but you really should not. A webservice is on demand. If it's called, it does work. If it's not called, it's not running.
You may have been thinking of a windows service. That is something that is always running and can do stuff in the background.
(A windows service may have an additional web frontend to see it's data. Or any other way to visualize it's data points, for example another database.)
As you tagged c# and asp so you are using Sql server database,
this query gives you databases that exists on your sql server instance:
select * from master.dbo.sysdatabases
result contains name and some extra information, name gives you database names, mode column have a int value indicates that database is in creating mode or created mode, status column that contains a int value(power of 2)
If you would like to see if database is offline or not, you can see status value if it is 512 your database is offline, for 1024 it is in read only mode
for your service you can use web api, web method or wcf, it is depend on you
If you use hangfire, quartz or any other scheduler you can set a background job on your server to check your database status
"return the result via a URL" I can not understand this, but if you want to notify users about database you can use push notification on your background job

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.

SqlDependency dependency_OnChange Info "Query"

I have a very frustrating problem. My .net 4 app has been successfully using the SqlDependency object for some time to receive INSERT or UPDATE notifications from SQL server.
However the database it receives notifications from has just been updated (it is the back end to a 3rd party app so I don't know what the update entailed) and now when I restart my SqlDependency app it does not behave properly. During the StartSQLDependency() method this line triggers the dependency_OnChange event:
using (SqlDataAdapter adapter = new SqlDataAdapter(_SqlCommand))
adapter.Fill(_datatable);
With SqlNotificationEventArgs:
e.Info "Query"
e.Source "Statement"
e.Type "Subscribe"
Previously I had only seen:
e.Info "Update" or "Insert"
e.Source "Data"
e.Type "Change"
Can anyone please suggest what my have changed in the database to cause this?
I know this is old, but for future generations...
In trying to research SqlDependency triggers, I came across a CodeProject that says that an event with those parameters indicates a bad query being used for the notifications (about halfway down the article).
Microsoft has an article about the supported notification statements (for SQL Server 2008) and how to properly build them to avoid this error event.

Categories

Resources