public void CreateJob(WAMSQLJob wamJob, string server = "localhost") { Server srv = null; if (server == "localhost") { srv = ConnectToLocalDatabase(_username, _password); } else { srv = ConnectToRemoteDatabase(server, _username, _password); } if (srv != null) { try { Job job = new Job(srv.JobServer, wamJob.Name); job.Description = wamJob.Description; job.Create(); JobStep jobStep = new JobStep(job, wamJob.JobStepName); jobStep.Command = wamJob.JobStepCommand; jobStep.OnSuccessAction = StepCompletionAction.GoToStep; jobStep.OnSuccessStep = 2; jobStep.OnFailAction = StepCompletionAction.GoToStep; jobStep.OnFailStep = 3; jobStep.Create(); JobStep successStep = new JobStep(job, "ReportSuccess"); successStep.Command = $"INSERT INTO WAM.dbo.JobHistory VALUES(NEWID(),{wamJob.JobStepName},'SQL',GETDATE(),'Success',null)"; successStep.OnSuccessAction = StepCompletionAction.QuitWithSuccess; successStep.OnFailAction = StepCompletionAction.GoToStep; successStep.OnFailStep = 3; successStep.Create(); JobStep failStep = new JobStep(job, "ReportFailure"); failStep.Command = $"INSERT INTO WAM.dbo.JobHistory VALUES(NEWID(),{wamJob.JobStepName},'SQL',GETDATE(),'Failure','Not Available'"; failStep.OnSuccessAction = StepCompletionAction.QuitWithSuccess; failStep.OnFailAction = StepCompletionAction.QuitWithFailure; failStep.Create(); JobSchedule jobSched = new JobSchedule(job, wamJob.ScheduleName); jobSched.FrequencyTypes = wamJob.ScheduleFrequencyType; jobSched.FrequencyRecurrenceFactor = wamJob.ScheduleFrequencyRecurrenceFactor; jobSched.FrequencyInterval = wamJob.ScheduleFrequencyInterval; jobSched.ActiveStartTimeOfDay = wamJob.ScheduleActiveStartTimeOfDay; jobSched.Create(); } catch (Exception ex) { throw new Exception("Failed To Create Job: " + wamJob.Name + " - " + ex.Message); } } else { throw new Exception("Failed To Connect to SQL Job Server"); } }
public void CreateJob(WAMSQLJob wamJob, string username, string password, string server = "localhost") { Server srv = null; if (server == "localhost") { srv = ConnectToLocalDatabase(username, password); } else { srv = ConnectToRemoteDatabase(server, username, password); } if (srv != null) { try { Operator op = new Operator(srv.JobServer, "WAM_Operator"); op.NetSendAddress = "WAM_Server"; op.Create(); Job job = new Job(srv.JobServer, wamJob.Name); job.Description = wamJob.Description; job.Create(); JobStep jobStep = new JobStep(job, wamJob.JobStepName); jobStep.Command = wamJob.JobStepCommand; jobStep.OnFailAction = StepCompletionAction.QuitWithFailure; jobStep.Create(); JobSchedule jobSched = new JobSchedule(job, wamJob.ScheduleName); jobSched.FrequencyTypes = wamJob.ScheduleFrequencyType; jobSched.FrequencyRecurrenceFactor = wamJob.ScheduleFrequencyRecurrenceFactor; jobSched.FrequencyInterval = wamJob.ScheduleFrequencyInterval; jobSched.ActiveStartTimeOfDay = wamJob.ScheduleActiveStartTimeOfDay; jobSched.Create(); } catch (Exception ex) { throw new Exception("Failed To Create Job: " + wamJob.Name + " - " + ex.Message); } } else { throw new Exception("Failed To Connect to SQL Job Server"); } }