Friday, February 24, 2012

Enable/Disable Job with SMO

I am trying to enable/disable a job using a using smo in a script task with the following code

Imports System.Object
Imports Microsoft.SqlServer.Management.Common
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Smo.Agent
Imports Microsoft.SqlServer.Management.Smo.SqlSmoObject
Imports Microsoft.SqlServer.Management.Smo.SmoObjectBase
Imports Microsoft.SqlServer.Management.Smo.NamedSmoObject
Imports Microsoft.SqlServer.Management.Smo.Agent.Job
Imports Microsoft.SqlServer.Management.Smo.Agent.Jobserver
Imports Microsoft.SqlServer.Management.Smo.Agent.AgentObjectBase

Public Class ScriptMain

Public Sub Main()

Dim strJobserver As JobServer

Dim strJob As Job
Dim srv As Server
srv = New Server("conchango-vpc")

strJob = New Job(srv.JobServer, "test")

strJob.IsEnabled = True

strJob.Alter()

Dts.TaskResult = Dts.Results.Success
End Sub

End Class
and i get an error at the alter point.

when code is changed to using the following line
srv.Jobserver.Alter()
it runs successfully but no change is made to the job.
What am I missing?
Ifaka Enefe
Conchango

Your code is trying to create a new job called "test" instead of using the existing job.

Instead of:
strJob = New Job(srv.JobServer, "test")

Use:
strJob = srv.JobServer.Jobs("test")
|||It worked. Thanks!|||Hi,
I've got the followinng inside a class library:


Server serv;

Job jb;

serv = new Server(this.strServerName);

jb = serv.JobServer.Jobs(this.strJobName);

if (strJobEnabledStatus=="True")

{

jb.IsEnabled = true;

}

else

{

jb.IsEnabled=false;

}

jb.Alter();



And it complains about


serv.JobServer.Jobs(this.strJobName)

saying "'Microsoft.SqlServer.Management.Smo.Agent.JobServer.Jobs' is a property but is used like a 'method'"

Any ideas? I aint much a developer I'm afraid so I'm at a bit of a loss here.

Thanks
Jamie|||s'all right. I've found it.


jb = serv.JobServer.Jobs[this.strJobName];

Must get my head out of VB mode Smile

-Jamie

No comments:

Post a Comment