set sql agent job schedule time in C#

2.5k views Asked by At

I want to schedule a group of queries run weekly/configurable.I found below code by googling but am trying to set sheduler timings for weekly execution now.Also i want it to be configurable. Am pretty new to this so somw help would be appreciated!

 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 
        job = new Job(jobServer, "Job_1");
        job.Create();
        job.AddSharedSchedule(schedule.ID);
        job.ApplyToTargetServer(server.Name);
        job.

        // Create JobStep 
        step = new JobStep(job, "Step_1");
        step.Command = "SELECT 1";
        step.SubSystem = AgentSubSystem.TransactSql;

        step.Create();

Provide links to the job scheduler examples.Am lost in finding how to set schedule timings!

1

There are 1 answers

0
PCG On BEST ANSWER

The FrequencyTypes Enum that you are using in the code should have a Weekly value.

http://technet.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.agent.frequencytypes.aspx