How to run an existing SQL Server Agent Job from C#
来源:互联网 发布:淘宝卖家怎么回评买家 编辑:程序博客网 时间:2024/05/01 18:42
If you want to execute an existing Microsoft SQL Server Agent Job in C# you an use the following function:
The function uses SQL authentication to connect to the Microsoft SQL Server instance.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.Agent;
using Microsoft.SqlServer.Management.Common;
using System.Data;
public override void Execute()
{
Server server = new Server(@"MyServer\MyInstanceName");
try
{
server.ConnectionContext.LoginSecure = false;
server.ConnectionContext.Login = "MyName";
server.ConnectionContext.Password = "MyPassword";
server.ConnectionContext.Connect();
Job job = server.JobServer.Jobs[Name];
job.Start();
}
finally
{
if (server.ConnectionContext.IsOpen)
{
server.ConnectionContext.Disconnect();
}
}
}
Add this after the above method to have the Job wait until it’s done
Thread.Sleep(5000);
job.Refresh();
while(job.CurrentRunStatus != JobExecutionStatus.Idle)
{
Thread.Sleep(5000);
job.Refresh();
}
Reference: http://www.roelvanlisdonk.nl/?p=1736
- How to run an existing SQL Server Agent Job from C#
- MS SQL Server - How to change an existing column to auto-increment column
- How to Run Linux from an USB Flash Drive
- How to run SSIS Project as a SQL Job
- in salesforce how to run batch from develop console and schedule job apex job
- How to run (git/ssh) authentication agent?
- To create an .edmx file from an existing database
- An example to create JOB in SQL Server
- How to Add Core Data to an existing Utility Application
- How to make a random selection from an SQL table
- [SQL Server]收集Agent job信息
- How to overlay an icon over existing shell objects
- How to run Cobertura as an ant build script from eclipse
- How to recover deleted data from SQL Server 暴强
- How to remove Duplicates from an Array
- How to load content from an IFile
- How to call Visual Basic .NET run-time library members from Visual C#
- How can I run an EXE from within my application?
- 字符设备管理机制分析(一)
- android 打开文件
- C++和C在linux下编程和与在WINDOWS下有什么区别
- 为什么不自己挖一个井
- Java 常用方法
- How to run an existing SQL Server Agent Job from C#
- 高并发系统设计
- 2011ACM上海邀请赛J题( Juice Extractor)----离散化+DP
- poj 1182 食物链 并查集
- POJ--3160[Father Christmas flymouse] 缩点+SFPA求最长路
- 解读Android LOG机制的实现:(1)LOG的实现架构
- symfony 中doctrine关系的论述
- java笔记总结_03_数组、继承和多态
- POJ--3592[Instantaneous Transference] 缩点+求最长路