How to run an existing SQL Server Agent Job from C#
2011-07-24 17:29
726 查看
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
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 automatic process SSAS cube using SQL Server agent job
- How to check the MS SQL Server job run status by using Script
- MS SQL Server - How to change an existing column to auto-increment column
- How to call Visual Basic .NET run-time library members from Visual C#
- How to Downgrade a Database from SQL Server 2005 to SQL Server 2000
- How to build an OLE DB application for SQL Server CE in Visual Studio 2005
- How to recover deleted data from SQL Server
- convert return char from sql server 2008 r2 or below version to c#
- An example to show how to make Tree structure data table in SQL server
- SQL Server Agent执行Job失败 提示[396] An idle CPU condition has not been defined - OnIdle job schedules will have no effect
- How To Build Multi-control Components when Inheriting from an Existing Control (Intro and TextBox Example)
- All-In-One Code Framework(AIO): 如何使用C#编写进程外的COM组件 (How to write an out-of-proc COM server in C#)
- How to troubleshoot SQL Server performance issues - zt from MS
- How to Find All SQL Server Instance Running in Local Network c#
- vSphere Howto: Remove a LUN from an ESXi server
- An example to create JOB in SQL Server
- How to Implement an Automatic Sliding Window in a Partitioned Table on SQL Server 2005
- How to make a random selection from an SQL table
- SQL Agent Job 报“Access to the remote server is denied because the current security context is not trusted”
- 好记性不如烂博客之 Quartz How-To: Update an existing job