Library tutorials & articles

SQL Distributed Management Objects Part 2

The Code

Now, lets go through the code step by step:

Private Sub Class_Initialize()

On Error Resume Next

NL = Chr$(13) & Chr$(10)

Set oSQLServer = New SQLDMO.SQLServer

oSQLServer.LoginTimeout = 10

End Sub

The main object is created when the class is initialized, similarly this object shall be deleted from the memory when the class is terminated.

Our main method is known as AddTask, this method will add a new task to the task scheduler, note we have not passed the parameters directly to function instead we have used the properties to get the input from the user.

Public Function AddTask()


   On Error GoTo errhandler
       
   oSQLServer.DisConnect


Disconnect the server if its already connected.
       
   If Server = "" Then
       ErrDesc = "You must enter server name."
       Exit Function
   ElseIf UserID = "" Then
       ErrDesc = "You must enter a valid User ID"
       Exit Function
   ElseIf Password = "" Then
       Password = ""
   End If

   
Get values of important parameters from the user, these values are needed to connect to the sqlserver.

   'Connect to the server!
   oSQLServer.Connect CStr(Server), CStr(UserID), CStr(Password)
   
   Dim oJob As New SQLDMO.Job
   Dim idStep As Integer

   
Idstep will be used to define the total number of steps to be included in the task.

   'Set the schedule name
   oJob.Name = JobID


Assign a name to the job.

   'objSQLServer.Executive.Tasks.Add oJob
   oSQLServer.JobServer.Jobs.Add oJob


Add the newly created job to the job server. The jobserver object exposes attributes associated with SQL server agent. SQL Server agent is responsible for executing the scheduled jobs and notifying operators of SQL Server error conditions or other SQL Server execution or job states.

   'Use the code below to change the task!!!
   oJob.BeginAlter
   
   'idStep = 0

   
Initially we have assigned a zero value to the step id. Because we intend to add two steps in our task, so we run a loop twice.

   For idStep = 0 To 2
       
       Dim oJobStep As SQLDMO.JobStep
       Set oJobStep = New SQLDMO.JobStep

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of SQL Distributed Management Objects Part 2.

Leave a comment

Sign in or Join us (it's free).

S.S. Ahmed S.S. Ahmed is a senior IT Professional and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic we...

Related podcasts

  • Stack Overflow: Podcast #28

    This is the twenty-eighth episode of the StackOverflow podcast, where Joel and Jeff discuss Windows Azure, SQL Server 2008 full text search, Bayesian filtering, porn detection, and project management — among other things. Jeff met the inestimable Joey DeVilla aka Accordion Guy...

Want to stay in touch with what's going on? Follow us on twitter!