Administer SQL Server remotely[3]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 sonicdater 的 blog

 

objsqlserver.connect server, userid, password


note we have passed three arguments to the connect method. the first argument is the name of the sql server to which you want to connect, the second argument is the user id required to log on to the sql server, and thehe third argument is the password required to log on to the sql server. if you provide correct parameters to the connect method, you will be connected to the sql server.


step 3

once you are connected to the sql server, you can make use of the newly created object's methods and properties to accomplish the task. our task is to create a new task in the sql scheduler so we are going to create a new task, and later we will set certain properties of this object.
 

dim objtask as sqlole.task
set objtask = createobject("sqlole.task")

now that the task object has been created, we need to add the task to the scheduler. define the task name by calling the name property of the task object, and then add this task to the sql server scheduler.
 

objtask.name = taskname
objsqlserver.executive.tasks.add objtask

after the task has been added to the scheduler, it's time to add some commands in the newly created task. you may want to create and run a task to delete particular records from a table at a particular time, or you may want to send an email to the site administrator on an exact date of the month. all of this can be done by assigning values to certain properties of the task object. look at the below statements:

objtask.beginalter
objtask.database = databasename
objtask.command = commandtext

objtask.frequencytype = sqlolefreq_onetime
objtask.activestartdate = cdate(executiondate)
objtask.doalter

before assigning values to the properties, you must call beginalter method, which tells the sql server that changes are about to be made to the task properties. actually, each change to a single property is a separate update to sql server. we use the beginalter method to group multiple property changes into a single unit. call the doalter method to commit the changes made to the object properties. you can also call the cancelalter method to cancel the unit of property changes.

assign a valid database name to the "database" property. this is the database in which you want to execute the task.

 

objtask.database = databasename

pass a valid transact sql statement to execute for the task you have created to the "command" property.

objtask.command = commandtext

in the original task code, we assigned a valid value to the frequencytype property, which is the primary frequency unit of time. more details are included in the source code files accompanying this article. please refer to the component's source code to see the different uses of the frequencytype property.

objtask.frequencytype = sqlolefreq_onetime


the above line of code is meant to run only once, therefore a date is assigned to the activestartdate property. the task will automatically execute on this date. activestartdate is the date before which this task is active. there is another property which i think should be mentioned here, activeenddate, the date and time after which the task is active.

objtask.activestartdate = cdate(executiondate)


using the code provided, you could create a task that would run on a daily basis, hourly basis, or only once on the date provided as a parameter. by viewing the attached source code in visual basic, a reader can see that it is thoroughly commented so the reader can understand the statements without frequently pressing f1 to discover a statement's meaning. the following is the "removetask" function that removes the named task from the scheduler:

public function removetask(byval task as variant)

..........................

    objsqlserver.connect server, userid, password

    objsqlserver.executive.tasks(cstr(task)).remove
    errdesc = "the task has been removed."

.........................

end function


本文关键:Administer SQL Server remotely
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top