运行SQL代理作业里面的C#代码 [英] Run C# code inside a SQL Agent Job

查看:215
本文介绍了运行SQL代理作业里面的C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码,需要在指定时间运行的每一天。该代码现在正坐在我的web应用程序的一部分。有2个存储过程来获取/保存数据,该代码使用。

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.

我如何设置的Microsoft SQL Server Management Studio中2008 R2来执行我的代码,以及在一个SQL代理作业存储的特效。我以前从来没有这样做,似乎无法找到的文档。

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.

推荐答案

最简单的方法是让.NET控制台应用程序这仅仅是你真正的代码坐在一个DLL或web服务或任何一个外壳。然后,在你的SQL代理作业,创建一个步骤是调用您的控制台应用程序类型为操作系统(CmdExec)的。节省您的SSIS的麻烦(这是有一个的麻烦,避免)。我也同意@Hasanain一个.NET PROC可能是另一种合理的选择。

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.

还有一件事要注意。 SQL代理的CmdExec将寻找一个整数返回代码,因此有你的公共静态INT主(字符串ARGS []){} 成功方法返回0和一些负数失败。或者,如果你抛出一个例外,那会工作得很好了。和SQL代理将记录从任何你扔了,因为控制台应用程序将它写入标准输出/标准错误的文本。

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.

这篇关于运行SQL代理作业里面的C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆