Administer SQL Server remotely

本文介绍如何使用Visual Basic 6创建ASP组件,利用SQL分布式管理对象(SQL - DMO)远程管理SQL Server任务。详细说明了添加和删除SQL任务的步骤,包括创建SQL Server对象、连接服务器、设置任务属性等,还提及项目编译、组件使用场景及代码测试方法。
Administer SQL Server remotely
By S.S. Ahmed

This article shows how to create tasks in the sql server remotely. 
 Advanced
 VB6, XP, W2K, Win9X
 Posted 5 Nov 2001
Articles by this author
Send to a friend
Printer friendly version
Views: 805  [This is an unedited reader contribution] [Modify this article][Delete this article]
FAQ
What's New
Lounge
Contribute
Message Boards
5 users have rated this article. result:
2.2 out of 5.

Sample Image - sqldmo.jpg

INTRODUCTION


Before starting, let's shed some light on the SQL Distributed Management Objects (SQL-DMO). ASP gets its functionality by using server components. In ASP we can combine scripts, HTML, and reusable server components to create scalable Web applications. These server-side ActiveX components can be developed in many languages, such as VC++, Java, and Visual Basic (VB).

I have selected VB to develop a component that will be used in ASP scripts to harness the power of SQL-DMO. SQL-DMOs are OLE automation-compatible COM objects. These objects and a set of properties and methods are used to write programs to administer multiple SQL Servers distributed across a network. Also, SQL-DMO is the foundation of SQL Enterprise Manager. In fact, SQL-DMO is a very powerful object model of SQL Server management. The obvious advantage of using a component encompassing SQL-DMO functionality is that you can manage your SQL Server from anywhere in the world.

Although, SQL-DMO is a complete set of objects and methods to manage the SQL Server remotely, in this article, we will only see how to add and remove SQL tasks in the Task Scheduler. I decided to write this article because I couldn't find a good article about using SQL-DMO in ASP. The article, which details how to leverage VB to create the ASP component, uses the following technologies:

-Visual Basic 6
-SQL Server
 

The Real Business

I have created a class named "Task." that contains all the code needed to implement the functionality.

This is the code from Task.cls:

Public Function AddTask()

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

    objSQLServer.DisConnect

    objSQLServer.Connect Server, UserID, Password

    Dim objTask As SQLOLE.Task
    Set objTask = CreateObject("SQLOLE.Task")

    'Set the schedule name
    objTask.Name = TaskName

    objSQLServer.Executive.Tasks.Add objTask

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

    Case "single_run":
    Case 2:

        If ExecutionDate = "" Then
            ErrDesc = "You must provide the task execution date."
            Exit Function
        Else
            If IsDate(ExecutionDate) = False Then
                ErrDesc = "Please provide a valid task execution date."
                Exit Function
            Else
                'Set the schedule name
                objTask.Name = TaskName

                objSQLServer.Executive.Tasks.Add objTask

                'Change the task!
                objTask.BeginAlter
                objTask.Database = DatabaseName
                objTask.Command = CommandText

                objTask.FrequencyType = SQLOLEFreq_OneTime
                objTask.ActiveStartDate = CDate(ExecutionDate)
                objTask.DoAlter
            End If
        End If

        If (objTask.CmdExecSuccessCode) Then
            ErrDesc = "Failure"
        Else
            ErrDesc = "Success"
        End If

End Function

The class has two main functions named AddTask and RemoveTask.AddTask adds a new task to the Scheduler. Similarly, RemoveTask removes the task from the Scheduler. First of all, you will have to include the "Microsoft SQL OLE Object library" from the references in the Project Menu. Once, you have done that, follow the steps below:


-Create a SQL Server object.
-Connect to the SQL Server object.
-Use the SQL Server object and other contained objects.
-Release the SQL Server object.

Step 1

The following creates a new SQL Server object:

Dim objSQLServer As SQLOLE.SQLServer
Set objSQLServer = New SQLOLE.SQLServer

The objSQLServer object is an instance of the SQLOLE.SQLServer class. This object represents the SQL Server in which tasks will be added or removed. It's needed in order to move ahead and create another object that will be used to create a new task. Notice this uses the "New" keyword to instantiate the SQLServer object. We could have used the CreateObject function instead, but late binding would have given the app a slower performance. The reference through an early bound variable promotes a better performance.
 

Step 2

The following connects to the SQL Server object:
 

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


Remember, it's necessary to set certain properties before calling this method. Have a look at the following statements:

Dim objTaskManager
Set objTaskManger = server.createobject("TaskManager.Task")

objTaskManager.Server = cstr(request.form("servername"))
objTaskManager.UserID = cstr(request.form("userid"))
objTaskManager.Password = cstr(request.form("password"))
objTaskManager.RemoveTask cstr(request.form("taskname"))

response.write objTaskManager.ErrDesc

Set objTaskManager = Nothing

This is the ASP code that shows how to instantiate the component and call the "RemoveTask" function. Notice that before the function is called, server name, User ID, and Password values are passed to the properties. Use the ErrDesc property to see the status of the function called.

Following is the ASP code needed to add a new task to the SQL Scheduler:

Dim objTaskManager
Set objTaskManager = server.createobject("TaskManager.Task")

objTaskManager.Server = cstr(request.form("servername"))
objTaskManager.UserID = cstr(request.form("userid"))
objTaskManager.Password = cstr(request.form("password"))
objTaskManager.DatabaseName = cstr(request.form("databasename"))
objTaskManager.TaskName = cstr(request.form("taskname"))
objTaskManager.CommandText = cstr(request.form("commandtext"))
objTaskManager.ScheduleType = cint(request.form("scheduletype"))
objTaskManager.ExecutionDate = cstr(request.form("executiondate"))
objTaskManager.AddTask

response.write objTaskManager.ErrDesc
Set objTaskManager = Nothing

The task created above will run only once because we have passed the current date to the ExecutionDate property.

Compiling the Project

After entering the code in the class module, compile the project to make a DLL. It would be better to reference the "Microsoft Active Server Pages Object Library" in the project so ASP will not give us an "Out of Process Component" error message if we try to call the component from any ASP page. In a nutshell, what this means is that this component will know that it needs to access the Active Server Pages DLL (asp.dll) to run. Finally, compile the project as a DLL. Voila! You have created an ASP component that can add and remove SQL Scheduler tasks.

Usage

Consider a scenario in which you create an e-commerce application. You have used SQL Server for data-storage purposes. Users come and register on your site before doing any shopping on your site, and they place orders, but leave your site without checking out.

You should be able to delete the items they placed in their shopping basket. It would be tedious to manually check the database for valid records and delete the unwanted records. Instead, use the component you just created and create a task that would search the database for invalid or unwanted records and would delete them automatically after a certain amount of time.

Similarly, you can create and run a VB application once a month that would remove the accomplished tasks from the SQL Server. Alternately, you could create a component that would check and remove the unwanted tasks from the SQL Server, create a new task manually, and call this component using the extended stored procedures provided with SQL Server to remove the unwanted tasks from the SQL Server. SQL Server 6.5 and later provides the capability of loading and executing COM objects through a set of OLE Automation stored procedures or through extended stored procedures.
 

Code Details


VB Component source code and ASP files to test the component are provided with this article. A compiled DLL is also provided. Directly plug in the DLL in your Web application to test the component functionality. Source code is also provided so that you can play with the code yourself and try to add some more features in the component. ASP files are provided to test the component in ASP. Create and remove tasks through these files. To run the ASP files, create a new Web application and include all the ASP files in the new application, and open the index file, tm_demo1.asp, in your browser to test the functionality.
 

Summary

Creating ASP Component to manage SQL Server remotely is fairly easy. It only requires a basic knowledge of ASP, VB, and SQL Server. The article shows how easy it is to create a component that harnesses the power of SQL-DMO and manages the SQL tasks remotely.

About S.S. Ahmed

S.S. Ahmed is a senior software engineer and works for a web and software development firm. Ahmed specializes in creating database driven dynamic web sites. He uses ASP and VB for most of what he develops. Ahmed likes to hop into other tools as well. Ahmed has worked in Assembly language, C, C++, FoxPro, QBasic, All version of Visual Basic (currently experimenting with VB.NET), Visual C, Java, ASP, etc. Ahmed enjoys travelling and has been to many parts of the world. Ahmed can be reached at ss_ahmed1@hotmail.com

代码转载自:https://pan.quark.cn/s/f87b8041184b Language: 中文 欢迎来到戈戈圈! 当你点开这个存储库的时候,你会看到戈戈圈的图标↓ 本图片均在知识共享 署名-相同方式共享 3.0(CC BY-SA 3.0)许可协议下提供,如有授权遵照授权协议使用。 那么恭喜你,当你看到这个图标的时候,就代表着你已经正式成为了一名戈团子啦! 欢迎你来到这个充满爱与希望的大家庭! 「与大家创造更多快乐,与人们一起改变世界。 」 戈戈圈是一个在中国海南省诞生的创作企划,由王戈wg的妹妹于2018年7月14日正式公开。 戈戈圈的创作类型广泛,囊括插画、小说、音乐等各种作品类型。 戈戈圈的目前成员: Contributors 此外,支持戈戈圈及本企划的成员被称为“戈团子”。 “戈团子”一词最初来源于2015年出生的名叫“团子”的大熊猫,也因为一种由糯米包裹着馅料蒸熟而成的食品也名为“团子”,不仅有团圆之意,也蕴涵着团结友爱的象征意义和大家的美好期盼,因此我们最终于2021年初决定命名戈戈圈的粉丝为“戈团子”。 如果你对戈戈圈有兴趣的话,欢迎加入我们吧(σ≧︎▽︎≦︎)σ! 由于王戈wg此前投稿的相关视频并未详细说明本企划的信息,且相关视频的表述极其模糊,我们特此创建这个存储库,以文字的形式向大家介绍戈戈圈。 戈戈圈自2018年7月14日成立至今,一直以来都秉持着包容开放、和谐友善的原则。 我们深知自己的责任和使命,始终尊重社会道德习俗,严格遵循国家法律法规,为维护社会稳定和公共利益做出了积极的贡献。 因此,我们不允许任何人或组织以“戈戈圈”的名义在网络平台或现实中发布不当言论,同时我们也坚决反对过度宣传戈戈圈的行为,包括但不限于与戈戈圈无关的任何...
内容概要:本文详细介绍了一个基于YOLOv8的血细胞智能检测系统全流程开发指南,涵盖从环境搭建、数据准备、模型训练与验证到UI交互系统开发的完整实践过程。项目利用YOLOv8高精度、高速度的优势,实现对白细胞、红细胞和血小板的自动识别与分类,准确率超过93%,单张图像检测仅需0.3秒。通过公开或自建血细胞数据集,结合LabelImg标注工具和Streamlit开发可视化界面,构建了具备图像上传、实时检测、结果统计与异常提示功能的智能系统,并提供了论文撰写与成果展示建议,强化其在医疗场景中的应用价值。; 适合人群:具备一定Python编程与深度学习基础,从事计算机视觉、医疗AI相关研究或项目开发的高校学生、科研人员及工程技术人员,尤其适合需要完成毕业设计或医疗智能化项目实践的开发者。; 使用场景及目标:①应用于医院或检验机构辅助医生进行血涂片快速筛查,提升检测效率与一致性;②作为深度学习在医疗影像领域落地的教学案例,掌握YOLOv8在实际项目中的训练、优化与部署流程;③用于学术论文写作与项目成果展示,理解技术与临床需求的结合方式。; 阅读建议:建议按照“数据→模型→系统→应用”顺序逐步实践,重点理解数据标注规范、模型参数设置与UI集成逻辑,同时结合临床需求不断优化系统功能,如增加报告导出、多类别细粒度分类等扩展模块。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值