SMO Sample: Create and Start a Job

Purpose of sample: Create simple job and start the job after creation.

 

Server svr = new Server(instance);
JobServer agent = svr.JobServer;

if (agent.Jobs.Contains("New Smo Job"))
{
agent.Jobs["New Smo Job"].Drop();
}

Job j = new Job(agent, "New Smo Job");
JobStep js = new JobStep(j, "Step 1");
js.SubSystem = AgentSubSystem.TransactSql;
js.Command = "select 1";
j.Create();
j.ApplyToTargetServer(svr.Name);

j.Start();

Disclaimer: this sample doesn't handle exceptions and may not function as expected. Use at own risk. It is good practice to test an application before using it in production.