0

I am using C# code to read all the SQL Agent Jobs:

static readonly string SqlAgentJobName = "SqlAgentJob_123";
static ServerConnection conn;
static Server server;
static Job job;

try
{
    conn = new ServerConnection(SqlServer); 
    server = new Server(conn); 
    job = server.JobServer.Jobs[SqlAgentJobName]; //Get the specified job
    GenerateScript(job); // I need idea to write this method.
}
catch (Exception ex)
{
    Console.WriteLine("Failed to fetch the job :" + ex.Message);
    throw ex;
}

How can I generate a script out of it, so that I can run it on any server? When I run the program, it should read all jobs from the server and generate a script.

Is there any built-in method? Will SQL Server Development Tools help?

0

You can use the Script method of the Scripter object in SMO to achieve that.

An example can be found here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.