How to catch SQL Server trigger in C# code - c#

I have to create an external service for existing database which is working with ERP system.
Edit: service will be running on the same machine where SQL Server is running.
Service has to listen for a trigger on a table with documents.
Scenario:
User creates a document
Trigger calls service method
Method queries the database to get data, create document and send it to external API.
Is it possible to catch trigger like that from a C# Worker Service?

It's technically possible to run arbitrary SQL CLR code in a trigger, and make remote web service or rpc calls to code running in a separate service. But since the trigger runs during the transaction and any code you run can delay or undo the database change, it's not recommended. And probably would not be supported by the ERP system.
So the best pattern here is to have the trigger write a row into a local table, and then have an external process poll that table and do the rest of the work, or to configure Change Data Capture or Change Tracking and have the external program query that.

Related

C# batch up operations to load in remote database asynchronously

I have several front end web app servers, running ASP.NET MVC apps, which collect some analytics data on visitors.
I want to collect this data into a central database, via a Web API call.
I don't want to lose data, but I also don't want the front end servers to slow or stop in the event that the webapi/db server is not available.
I'm envisioning writing all these events to a log file and then run an async process to ship them to the central webapi/db, and can retry in case the server is not available for a time.
Is there some standard library or method for performing this, without building a bunch of custom code around it?

Periodically invoking of c# application using job in SQL server

I want to invoke a c# application based on SQL server database. I have name's column in database. I want to check if there is any name present in the database then every after 1 hr my C# application should get invoked. I am asked to use a "job" concept in SQL server. Is it possible to perform this operation?
Job schedules are designed to do maintenance staffs. They actually do something periodicall, but the things they do is about backups, running T-SQL jobs, CleanUp and etc. I don't think doing this by an SQL job agent be a good solution.
If you want to check db in a constant interval, then you could simply write an intermediate app (windows service or a console App) and check your db changes every 1 hr. If your desired changes happened then invoke your C# application.
You could call a windows application using a shell. You could do it in an SQL server job or in a trigger.
Hope I helped!

Creating a Database Query Service

I've been asked to create a Service for our Parent Company. They don't care how I do it, as long as the data is sent to them.
We have an SQL 2000 Server that receives machine data via Data Transformation Services (DTS).
Our Parent Company wants me to create a Service that runs every 5 minutes or so to collect new data, summarize it, and forward it to them.
With my background in Windows Forms development, I naturally think that I should poll the database every 5-minutes using some type of Windows Service, then send that data over to our Parent Company.
The machine housing this data is an old Windows 2000 machine, and our Network Administrator has recommended that I write this as a Web Service on our newer Web Server.
I created a Web Service a few months back for the Web Server to pull work order information from our Parent Company, but I do not know how to make this Service execute a process every 5-minutes.
Yesterday, I learned how to create an Insert After Trigger when records were added to the table. Unfortunately, the triggers are not called because this old server uses DTS jobs. I was able to learn about Controlling Trigger Execution When Bulk Importing Data, but there does not seem to be a way to modify our old DTS jobs to enable the BULK INSERT command. It may not work on SQL Server 2000.
So, with this background, should I create a Windows Service or a Web Service?
How should I proceed?
I would not make a web service for a recurring task. Web services are not very comparable to a windows service.
btw: A simpler alternative might be to create a command-line app that runs, periodically via a scheduled task (read about the "AT scheduler in Server 2000"). I is just easier to install and make updates because it wouldn't require a reboot of your server each time you make an update.
If the webservice has a method which you can call that executes the data importing/converting exactly one time, you can use a windows task or cron job to make a request to that method. You can either add this task to the server that is hosting the service, or some other server as long as it can access the webservice.

Paradox - know whether there is change in the database without opening a connection

I am writing a application in C# that needs to do the following:
without connecting to the database I need to check if there are some new logs in database. If there are then I am allowed to open the connection and retrieve them.
So I just need to know if there are new logs (elements) in the database WITHOUT opening the connection to it.
Server can send mail to administrator and I could monitor mailbox for changes but that solution is unacceptable.
Can server on inserting new rows create *.txt file on disk with text indication new rows which I can check and delete/edit after downloading change?
(database is in SQL Server 2008 R2)
Is it even possible? Any/And/Or other options to do this are welcome.
Thank you all very much in advance.
Based on the following clarifying comments from the OP under the question:
There is Web application which checks for change every 30 sec and shows latest authorizations. Database is tracking employee authorization and has frequent updates. Now I'm building desktop application, which has local connection to the server and can update more frequently, but client does-not want application to open connection every sec, aldo connection is opened for several ms.
I think that the appropriate solution is a business layer.
If you build a business layer hosted in IIS that performs the database access on behalf of the users using a single database user for access (the application pool user or an impersonated user within the web application), then connection pooling will reduce the number of connections made to the database significantly.
Here is an MSDN article that describes the mechanics and benefits of connection pooling in great detail.
All of the clients, including the web layer, would connect to the business layer using WCF or .Net Remoting (depending on your .Net version), and the business layer would be the only application performing database access.
The added benefit of this approach is that you can move all database access (including from the web client) inside the DMZ so that there is no direct database access from the DMZ outward. This could be a good selling point for your customer.
We use this mechanism extensively for very large, very security and performance conscious customers.
Update
As an alternative, you could have the business layer query the database every 30 seconds, extract the necessary information, and store it locally to the business layer in a database of some sort (Access, Sql Server Express, etc). When requests from clients are received, they will be served from the local data store instead of the database.
You could do this by kicking off a background thread in global.asax's Application_Start event or by adding a cache entry that expires every 30 seconds and performing the work in the cache timeout event.
This will reduce the number of connections to 1 (or 2 if the web isn't modified) every 30 seconds (or whatever the time is).
Try to monitor files change date inside DB folder.
If the client desktop application is not going to be deployed massively you could use SqlDependency. Then you wouldn't have to poll the database on a frequent basis, instead the database will notify you if something changes.
You could also deploy a service on the server which uses SqlDependency and then connect to this service from your desktop applications.
If that's not an option this document mentions some other options.
These two could be applied to your situation:
Create an AFTER UPDATE trigger on the table being monitored, whose action uses SQL Server Service Broker to send a message to the entity needing the notification.
Use Windows Server App Fabric Cache, which supports a change notifications mechanism, based on an in-memory object cache and callback functions you register with the objects.

trigger winforms program to run whenever a record is inserted in SQL Server via web app

I need to trigger a Winforms program that runs in taskbar tray whenever a row is added in SQL Server database from a web app. Is there a way to do this?
At the moment, the program runs all the time checking the database for new record and sleeps the underlying thread if nothing is inserted, but I need also to be able to trigger the program to run whenever a user inserted a row from a web app front-end.
added: web app, database, and winforms are running on the same one server.
thanks
"Standard" ways of triggering additional actions from inserts in SQL Server:
If the additional action is local to the database, and entirely implementable in transact SQL, the recommendation would be just to write it in a trigger.
If the additional action isn't implementable in transact SQL, then you might consider writing a CLR trigger. However, again I'd say that this is only appropriate if whatever is going to happen is local to the database.
If the additional action is appropriate to the server, but relies on e.g. other databases, or other features outside of the server, then you'd generally want to decouple the additional actions from the original INSERT operation. Two ways of dealing with this are SqlDependency, as others have said, or Service Broker.
If you're going the route of SqlDependency, you need something running permanently to use this object. This is what you'd typically put inside a windows service.
Try to use SqlDependency.
Also check this link Query Notifications in SQL Server (ADO.NET)
If you can refactor your application a bit, you could potentially create a duplex wcf service contract. here is a tutorial on that http://msdn.microsoft.com/en-us/library/ms731184.aspx
This is one of the message patterns available in WCF. You can use it to for doing things like firing events from the server, which the clients can subscribe to, which i think might be able to use.

Categories