Periodically invoking of c# application using job in SQL server - c#

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!

Related

How to catch SQL Server trigger in C# code

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.

A way for a program to constantly run

I am writing a website that will be using an SQL database. However I am needing the database to be updated with real-time information. I have no problems doing this as I have written a C# program to do this. However as I want this data to be updated 24/7. It's not appropriate to run it on my own machine. Any suggestions would be greatly appreciated!
There are many options:
If you have a dedicated server:
Create your application as a console application and put it in startup
Create your application as a console application and using Windows Task Scheduling run it with your required frequency.
Create it as a windows service that will always run
If you don't have a dedicated server:
Use libraries like Quartz to perform task scheduling in your web application
Assuming that by saying SQL you mean Microsoft SQL Server...
As already told the optimal solution is to code a Windows service. In case you are working with express edition of visual studio this can be tricky without the related template which is available only on paid edition or with 2017 community , so a possibility is to make an executable program and activate it using task scheduler and the repeat every option.
To be honest I would prefer to keep everything inside SQL , using SQL server agent you can execute jobs defined among SSIS , basically a rebranded and extended DTS which can easily operate on DB, have a better logging than the primitive OS task scheduler. No need to go on was or cloud, it is just a SQL server function that need to be enabled by the DBA, if not already used.

Configure web service and method to run on a periodic basis

I would like to automate a web method inside a web service that should run on a periodic basis. I also need to pass some arguments to it at runtime.
Some details:
My development machine has Visual Studio 2012 Professional and IIS
Express installed.
The production server has IIS 7.
SQL Server Express 2008 as RDBMS.
The arguments should be taken from a table in
the local DB and are subject to change.
Two of the fields in the table (execution_periodic_basis and execution_day) are used to indicate when the web method is supposed to run, i.e., the day can be MONDAY through SUNDAY for WEEKLY runs, or 1 through 28 for MONTHLY runs.
What would be the best approach to accomplish this? If you, based on your experience, have any suggestions on a better way to perform this process, they will be greatly appreciated.
Any tips or links will be more than welcome.
I'd recommend using IIS7.5 AutoStart http://msdn.microsoft.com/en-us/library/ee677260(v=azure.10).aspx site which you then use to Poll the SQL Server database. This AutoStart app (the web equivalent of a Windows Service) can then call your designated URL when the schedule matches its log and the current time.
Sounds like you'll need to either set timers (so look ahead for the next checkup, and set them), or once every x amount of time, see if it needs to be done.
I'd probably use a background thread with a timer and sleep in between.
Having said that, I'm not sure this question will get much love, as it's not the best "fit wise".
Use a Windows Service. Web Services are not designed for this task!

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.

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