Create SQL Agent …

Introduction

This sample code can be used to create SQL Agent job by using SQL Server Management Objects ( SMO ) http://msdn.microsoft.com/en-us/library/ms162169.aspxYou can use this code sample C# application as a reference to create a simple SQL Agent T-SQL job that runs on a one-time schedule.  

Building the Sample

Prereq: Visual Studio 2010 SP1, SQL 2012 Management tools

Description

 Sample code to create SQL Agent job using SMO. You can use this code sample C# application as a reference to create a simple SQL Agent T-SQL job that runs on a one-time schedule.  You need Visual Studio 2010 SP1, SQL 2012 Management tools

Source Code Files

  • CreateAgentJob.cs  - sample SMO code

More Information

Additional details: http://blogs.msdn.com/b/sqlagent/

 

C#
  
 
using System; 
using
 System.Collections.Generic; 
using
 System.Linq; 
using System.Text; 
using
 Microsoft.SqlServer.Management.Smo; 
using
 Microsoft.SqlServer.Management.Smo.Agent;   
namespace
 SQLAgentJobs  {
     class CreateAgentJob      {
         static void Main(string[] args)          {
              Server server = new Server(".");                // Get instance of SQL Agent SMO object              JobServer jobServer = server.JobServer;
              Job job = null;
              JobStep step = null;
              JobSchedule schedule = null;
               // Create a schedule              schedule = new JobSchedule(jobServer, "Schedule_1");              schedule.FrequencyTypes = FrequencyTypes.OneTime;              schedule.ActiveStartDate = DateTime.Today;              schedule.ActiveStartTimeOfDay = new TimeSpan(DateTime.Now.Hour, (DateTime.Now.Minute + 2), 0);
             schedule.Create();
               // Create Job
      
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章