sql job to run every certain time and execute a console application - c#

I am trying to write a SQL job to run every 10 minutes and to execute a console application that I wrote in C#.
It would be really helpful if someone could help.

What you want is a SQL Server Agent Job, with a CmdExec step (which can execute a windows command line). This is explained here: http://msdn.microsoft.com/en-us/library/ms190264.aspx
If security is a concern, it can be secured through the use of a SQL Agent Proxy definition. This is explained here: http://msdn.microsoft.com/en-us/library/ms189064(SQL.105).aspx

Related

How to create background application running on Window Server

I need to write an application that run in the background (i.e., invisible to user). It should be always running when the server is on, regardless of user login or off. Or at least it should run on schedule (e.g., hourly). It also should still run after the server shut down and turn on again.
The application is used to backup some data from one server (linux) to the local server where the application run (windows server).
From my research, many suggest to use Window Service. But I'm newbie on C# and also on this area like Window Service.
Can anyone direct me where I should start?
Is Window Service a suitable solution? Or if there is better solution? Please explain.
Thank you in advance.
[CLOSED]
Thank you for all who has responded.
Yes, the best practice is windows services.
However, if your operations are simple enough to write in DOS batch file. You can schedule your task in windows task scheduler. It will save a lot of time and is easy to setup.
UPDATE (FYI)
I have a batch_update.sql file that needs to be executed at 1.00AM every day. I created batch_update.sql file and a batch file batch_update.bat in C:\bat.
batch_update.sql includes all SQL operations.
batch_update.bat calls batch_update.sql file to execute it as follow.
sqlcmd -U adminuser -P password -S (local) -i C:\bat\batch_update.bat -o C:\bat\batch_update.txt
Then I created a task in windows task scheduler to run at 1.00AM which works pretty well.
A windows service definitely sounds like the long term solution. For simplicity however, you could create a command line project in C# and then use windows scheduler to run it in whatever intervals you like.
I recommend windows service too, you can have some information right here.
Yes, Windows Service is the solution you're looking for. Here's a good starting point on MSDN.

Activating a process automatically when the application is not running

I'm working on program which rergisters and processes service calls. I'm working in asp.net c# and my data base is on SqL Server. Each call has a status, at first 'NEW' and when work has started on it becomes 'UNDER CARE' and when work is finished 'CLOSED'. After the day is finished in the middle of the night when the application is not running, I want an automatic summing up to be made of the number of calls of the different types of status. How can this be done thru the application or thru the server or any other way even though the application isn't running?
What you described is a SQL Server Agent job scheduled to run at midnight.

How to run C# Console application from SQL Job in SQL Server 2008?

I want to try out to run my C# console application from SQL Job. So to test it, I simply created a console application and using C# and SMO, wrote few lines to create a database. I could run it successfully and it creates a database in the SQL Server as expected.
Then in IDE, I clicked on Build-->Publish myProject to E:\myFolder\MakeNewDB24 because that's where my SQL Server resides on.
The above action copied the following files to the specified location i.e., E:\myFolder\MakeNewDB24
Application Files
setup.exe
myProject.application
Then I opened my SQL Server, created a job by
rt. clicking Jobs Folder-->New Job.
I filled all the information in General.
In Steps, I have under Command,
\\mySQLServer\myFolder\MakeNewDB24\setup.exe
Type: Operating System(CmdExec)
Run as: SQL Server Agent Service Account
I ran the job. It showed the result as "Success"
When I viewed the History,
Executed as user: mySQLServer\SYSTEM. The step did not generate any output. Process Exit Code 0. The step succeeded.
I was happy. But when I went to check the database that was supposed to be created, its not available, meaning the SQL JOb didn't do its job to create my db.
I don't know what am I missing here? If anyone has knowledge on this, please kindly share with me. I really really want to see my SQL Job do this work. The reason I m using SQL job is because all of my automation tasks start from here. Thanks.
Your problem almost certainly has to do with permissions. You need to check the user id used for running the SQL Server Agent job. My guess is that it doesn't have permissions somewhere along the line.
The reason that your job returns success is because CMD jobs always return success if they successfully launch the CMD executable. It has nothing to do with whether or not your code succeeded.
In environments where I don't have to worry about security, I have gotten into the unfortunate habit of giving everything admin privileges, so I don't have to worry about what privileges are needed where. Good luck.

C# database connection delay

I have an app, it's done and working, but the users report a small problem with it, and quite frankly i susspected it could be a problem.
As the title says the app is written in c# (.net 3.5) and it's using SQL DB.
The "problem" is - when the app is first started, it takes about a half a minute to conenct to database.
Could this be somehow reduced?
Thank you for your time!
EDIT1:
the DB is local.. sql server and .net framework are installed at the same time as the app
EDIT2:
when the app is stared it all works fine, and when users open the winform where some DB work needs to be done, the first time it's started they have to wait about half a minute, after that it works fine..
From questions I asked in comments and their respective answers, the conclusion I offer is this:
It seems to me that the server agent service may be set with a startup mode of Manual. This means the service will only start as and when required by an application:
Manual starts a service as required or when called from an application (according to definition, but only some of the time in practice, depending on the service)
To mitigate this you could create a custom install action which would, as part of your product installation, set this startup mode to Automatic - this would cause the service to start when Windows starts.

The best way of triggering a timely event

I am building an ASP.Net website, I have a db table which I want to reset a column in it for all rows. column type: byte
I want to do this every day at midnight, in an automatically way. I know I can set a job in SQL Server Management Studio but I want to do this in a programmatic way and my website will be the trigger for it.
I'm using C#.Net 2008 and MS SQL Server 2005
i.e. (Pseudo code)
if(new_day)// can we be accurate that time here will be around 12:00:05 at maximum?
// call sql stored procedure to reset that column
You could make a simple Windows service in C# that would run in the background but considering all you want to do is make database updates, it's best to keep it in the DB....I'd suggest going with the scheduled job in SQL
Your website only executes when a page is requested making it unable to act as a service that executes a task at regular intervals. The best solution is to use SQL Server directly to schedule the task, create your own service or execute an application at regular intervals using Windows task scheduler.
However, if you for are in hosted environment you may not be able to do any of this. In that case you can use the cache to simulate a service. Omar Al Zabir has an article on CodeProject that explains how you can do that: Simulate a Windows Service using ASP.NET to run scheduled jobs.
If you really want to: create a Scheduled Task that calls your website every day at midnight. The website itself cannot trigger itself, but a task can do this.
But really: just set up a SQL job.
SQL Agent job, scheduled to run at the specified time.

Categories

Resources