Overview
SQL Distributed Management Objects (SQL Distributed Management Objects, SQL-DMO) extend SQL Server functionality by providing developers with methods to perform common tasks in both program and scripting languages. This article discusses how to use SQL-DMO to create ASP applications for database backup and validation.
Premise
You need to have knowledge of SQL Server database backup. Also add a reference to the SQL-DMO library in the Global.asa file. The following is a reference to SQL Server 2000:
<!--METADATA type= "TypeLib" name= "Microsoft SQLDMO Object Library" uuid= "{10010001-e260-11cf-ae68-00aa004a34d5}" version= "8.0"-->
The sample code for this article applies to SQL 7.0, MSDE, and SQL Server 2000.
Joy and Sorrow
Using SQL-DMO objects makes people happy and worried. It provides a lot of functionality so that you don't know how to use it. This article only discusses the object properties and methods involved in the example. You can find a lot of relevant information on SQL Server online teaching. The sample source code for this article can be downloaded at the end of this article.
SQLDMO. Sql server
The code looks very familiar. It is used to connect to a SQL Server database:
<%
Dim SRV
Set srv = Server.CreateObject ("SQLDMO.") SQL Server ")
Srv. LoginTimeout = 15
Srv. Connect "servername", "username", "password"
%>
This connects the SQL Server database by substituting the username and password. If you want to use NT authentication, set its LoginSecure property to True (true), ignore the incoming username and password, and use NT's registration information.
SQLDMO. Database
Lists the databases in the server. In the example of this article, the database that is listed is backed up. The following code lists the database in the server in the Drop-down menu:
<%
Dim SRV
Dim Objdb
Set srv = Server.CreateObject ("SQLDMO.") SQL Server ")
Srv. LoginTimeout = 15
Srv. Connect "servername", "username", "password"
Set objdb = Server.CreateObject ("SQLDMO. Database ")
%>
<select name= "Fdatabase" >
<%
For each objdb in SRV. Databases
If Objdb.systemobject = False Then
%>
<OPTION><%=objDB.Name%></OPTION>
<%
End If
Next
%>
</SELECT>
SQLDMO. BackupDevice
Lists the backup devices installed on the server. I recommend using a backup device to back up the database. This allows you to use the SQL-DMO checksum to verify the backup. The following code lists the backup devices on the server:
<%
Dim SRV
Dim Objdevice
Set srv = Server.CreateObject ("SQLDMO.") SQL Server ")
Srv. LoginTimeout = 15
Srv. Connect "servername", "username", "password"
Set Objdevice = Server.CreateObject ("SQLDMO. BackupDevice ")
For each objdevice in SRV. BackupDevices
Response.Write Objdevice.name + "<BR>"
Next
%>
SQLDMO. Backup
This is the backup core object we want to use. It has a number of properties that let us do the same level of backup as the enterprise SQL Manager. Let's discuss the properties used in this example.
BackupSetName-backup filename.
DB-the database to back up.
Action-Full or incremental backup. There are other options, but only these two are used in the example.
BackupSetDescription-Backup description.
Files-file backup options. Indicate the path and name of the backup file, such as: C:\pubs.bak. When you use a file backup, the following backup appliance name is set to NULL.
Devices-Backup device on the server. If you are using a backup device, the file backup option above is set to NULL.
TruncateLog-BACKUP LOG option. The options are:
NoLog-Do not back up the transaction log.
Notruncate-Backup transaction log. The time tag is provided in the log.
Truncate-Backs up the transaction log, but does not keep transactions.
Initialize-If set to True (TRUE), the backup device will be preferred as a replacement for other backup media.
The following is the backup.asp file in the example:
<%@ Language=vbscript%>
<HTML>
<BODY>
<!--contains all the login information-->
<!--#include file=login.asp-->
<%
Dim Objbackup
' Create a Backup object
Set objbackup = Server.CreateObject ("SQLDMO. Backup ")
' Set properties
Objbackup.backupsetname = Request ("fname")
Objbackup.database = Request ("Fdatabase")
Objbackup.action = Request ("faction")
Objbackup.backupsetdescription = Request ("Fdescription")
Objbackup.files = Request ("Fbackupfile")
Objbackup.devices = Request ("Fdevice")
Objbackup.truncatelog = Request ("Flog")
Objbackup.initialize = Request ("Finit")
' Back up the database
Objbackup.sqlbackup SRV
' Disconnect from the server
Srv.disconnect
' Release
Set srv = Nothing
Set objbackup = Nothing
%>
<P>
The backup was started and use the <a href= "devices.asp" >verify</A>
option to the if it completed successfully.
<a href= "Default.asp" >click here</a> to return.
</P>
</BODY>
</HTML>
Backup checksum
If you use VB or C + + programming, you can use event triggering to verify the backup process, but not in the ASP. We use the Sqldmo.backupdevice object's ReadBackupHeader method to confirm the success of the backup.
The following is the Verify.asp file code that lists the name of the backup appliance and provides information about the most recent backup.
<%@ Language=vbscript%>
<HTML>
<BODY>
<!--Login information-->
<!--#include file=login.asp-->
<P>
<%
Dim Objdevice
Dim Objresults
Dim icount
Dim Xcount
' Create a Backup device object
Set Objdevice = Server.CreateObject ("SQLDMO. BackupDevice ")
' Loop until a matching device is found.
For each objdevice in SRV. BackupDevices
If objdevice.name = Request ("fname") Then
' Find the matching device and start reading the results
Set objresults = Objdevice.readbackupheader
For icount = 1 to Objresults.rows
For xcount = 1 to Objresults.columns%>
<b><%=objresults.columnname (Xcount)%></b>:
<%=objresults.getcolumnstring (Icount,xcount)%><br>
<%next%>
<HR>
<%next%>
<%end if%>
<%Next%>
<%
Srv. Disconnect
Set srv = Nothing
Set objdevice = Nothing
Set objresults = Nothing
%>
</BODY>
</HTML>
The ReadBackupHeader method returns the QueryResults object. You can get the number of records backed up with its rows property. Then make a list of each row of records to search for information.
Other features
SQL-DMO also provides remote backup and recovery capabilities. This article does not cover database recovery, but SQL-DMO has a strong recovery capability.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service