SidAhmed
SidAhmed

Reputation: 2362

Schedule SSIS Package using C#

What I want to do, is to schedule a SSIS package, using C#. the missing part is, how to tell the agent that this schedulare is for the SSIS package "X", Here is my code :

Server srv = new Server();

//Define an Operator object variable by supplying the Agent (parent JobServer object) and the name in the constructor. 
Operator op = new Operator(srv.JobServer, "AC_Operator") { NetSendAddress = "Network1_PC" };

//Create the operator on the instance of SQL Server Agent. 
op.Create();

//Define a Job object variable by supplying the Agent and the name arguments in the constructor and setting properties. 
Job jb = new Job(srv.JobServer, "AC_Job");

//Specify which operator to inform and the completion action. 
jb.OperatorToNetSend = "AC_Operator";
jb.NetSendLevel = CompletionAction.Always;

//Create the job on the instance of SQL Server Agent. 
jb.Create();

//Define a JobStep object variable by supplying the parent job and name arguments in the constructor. 
JobStep jbstp = new JobStep(jb, "AC_Job_Step");
jbstp.OnSuccessAction = StepCompletionAction.QuitWithSuccess;
jbstp.OnFailAction = StepCompletionAction.QuitWithFailure;

//Create the job step on the instance of SQL Agent. 
jbstp.Create();

//Define a JobSchedule object variable by supplying the parent job and name arguments in the constructor. 

JobSchedule jbsch = new JobSchedule(jb, "AC_Job_Schedule");

//Set properties to define the schedule frequency, and duration. 
jbsch.FrequencyTypes = FrequencyTypes.Daily;
jbsch.FrequencySubDayTypes = FrequencySubDayTypes.Minute;
jbsch.FrequencySubDayInterval = 30;
TimeSpan ts1 = new TimeSpan(9, 0, 0);
jbsch.ActiveStartTimeOfDay = ts1;

TimeSpan ts2 = new TimeSpan(17, 0, 0);
jbsch.ActiveEndTimeOfDay = ts2;
jbsch.FrequencyInterval = 1;

DateTime d = new DateTime(2003, 1, 1);
jbsch.ActiveStartDate = d;

//Create the job schedule on the instance of SQL Agent. 
jbsch.Create();

Thanks for your time.

Upvotes: 2

Views: 1549

Answers (1)

billinkc
billinkc

Reputation: 61221

What you are needing to do is set the jobstep's SubSystem and then build your Command. In comparing the .NET generated code to a SQL Agent created job, the only difference I noticed was the assignment of the DatabaseName property so I set that as well.

You'll also undoubtedly want to look at dtexec to figure out how to configure and invoke your package or you can cheat like I do and use dtexecui or the Agent to build out the SET and other commands and then paste those in as the source command.

        //Define a JobStep object variable by supplying the parent job and name arguments in the constructor. 
        JobStep jbstp = new JobStep(jb, "AC_Job_Step");
        jbstp.OnSuccessAction = StepCompletionAction.QuitWithSuccess;
        jbstp.OnFailAction = StepCompletionAction.QuitWithFailure;

        string command = string.Empty;
        command = @"/FILE ""C:\sandbox\SSISHackAndSlash2008\SSISHackAndSlash2008\EzAPI_Recipe01.dtsx""  /CHECKPOINTING OFF /REPORTING E";
        jbstp.SubSystem = AgentSubSystem.Ssis;
        jbstp.DatabaseName = "master";
        jbstp.Command = command;

        //Create the job step on the instance of SQL Agent. 
        jbstp.Create();

Upvotes: 1

Related Questions