Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run C# code inside a SQL Agent Job

I have a piece of code that needs to run every day at a specified time. The code right now is sitting as a part of my web application. There are 2 stored procedures to get/save data that the code uses.

How can I setup Microsoft SQL Server Management Studio 2008 R2 to execute my code as well as the stored procs in a SQL Agent Job. I have never done this before and cannot seem to find the documentation.

like image 719
Tom Avatar asked Jul 11 '11 15:07

Tom


People also ask

Can I run C in Terminal?

Method 1: How to run C programs in Linux terminal In order to run a C program in Linux, you need to have a C compiler present on your systems. The most popular compiler is gcc (GNU Compiler Collection). Keep in mind that it is optional to provide the output object file (-o my_program).

Can Windows run C?

You will need two things to create C programs: a text editor to write the source code for the program and a compiler to convert the source code to an executable file so the program can be run (on Windows, executable files have a ".exe" extension).

Can I run C in Notepad ++?

Write and save the programTo write the source code of your first C program you need to open the Notepad++ text editor. The quickest way to do that in Windows 10 is to hit your Win key, type Notepad++ in the search window, and hit Enter. and paste it into the editor. Yes, this is your first C program!


2 Answers

The simplest method is to make a .NET console application that is just a shell for your real code sitting in a DLL or webservice or wherever. Then, in your SQL Agent job, create a step that is of type "Operating system (CmdExec)" that calls your console app. Saves you the hassle of SSIS (and that is one major hassle to avoid). I also agree with @Hasanain that a .NET proc might be another reasonable alternative.

One other thing to note. The SQL Agent CmdExec will look for an integer return code, so have your public static int Main(string args[]) {} method return 0 for success and some negative number for failure. Or, if you throw an exception, that'll work just fine too. And the SQL Agent will log the text from whatever you threw since the console app will have written it to stdout/stderr.

like image 84
mattmc3 Avatar answered Sep 22 '22 01:09

mattmc3


You should read up on Sql Server Integration Services (SSIS). You can then schedule SSIS packages which are units of sql functionality. Within an SSIS package you can run script jobs and call CLR (Common Language Runtime - i.e. .Net jobs) functions to execute your .net code.

One thing though, you may be thinking about this in a slightly backwards fashion. Is the primary reason for using SSIS to schedule code executions which call some sql? If so, I'd recommend you research / use Windows Workflow Foundation (WWF). This is a .net framework for developing and running "long term" .net activities. On a simple level, you can think of it as the equivalent of SSIS for .Net programs. It can be built directly into your .Net applications without any sql server SSIS knowledge.

Finally, if your application is "very" simple you may wish to consider just wrapping up the database update calls in a simple Console Application. That way you could simply call the application via the Task Scheduler built into Windows to run at certain days / times etc.

like image 21
Brian Scott Avatar answered Sep 22 '22 01:09

Brian Scott