Using ASP to back up and restore SQL Server database _ database related

Source: Internet
Author: User

An example code that backs up and restores a SQL Server database with ASP, hoping to be useful to some friends. (Note: Database may not be recoverable in use)

Copy Code code as follows:

<HTML>
<HEAD>
<title>sql Server Database backup and Recovery </TITLE>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
</HEAD>
<BODY>
<form method= "POST" name=myform>
Select actions: <input type= "Radio" name= "act" id= "act_backup" value= "Backup" ><label for=act_backup> backup </label >

<input type= "Radio" name= "act" id= "Act_restore" value= "restore" ><label for=act_restore> recovery </label>

<br> database name: <input type= "text" name= "DatabaseName" value= "<%=request (" DatabaseName ")%>" >

<br> file path: <input type= "text" name= "Bak_file" value= "C:\$1.bak" > (file path for backup or recovery) <br>

<input type= "Submit" value= "OK" >

</form>

<%

' Backup and restore of SQL Server database!

' Hai Eva

' Http://www.51windows.Net

Dim sqlserver,sqlname,sqlpassword,sqllogintimeout,databasename,bak_file,act

SQL Server = "localhost"

Sqlname = "sa" User name

SQLPassword = "sa" password

sqllogintimeout = 15 ' Login timeout

DatabaseName = Trim (Request ("DatabaseName"))

Bak_file = Trim (Request ("Bak_file"))

Bak_file = replace (Bak_file, "$", databasename)

act = LCase (Request ("act")

if DatabaseName = "" Then

Response.Write "Input Database name"

Else

If act = "Backup" Then

Set srv=server.createobject ("SQLDMO. SQL Server ")

Srv. LoginTimeout = Sqllogintimeout

Srv. Connect Sqlserver,sqlname, SQLPassword

Set bak = Server.CreateObject ("SQLDMO.") Backup ")

Bak. Database=databasename

Bak. Devices=files

Bak. Files=bak_file

Bak. SQLBackup SRV

If Err.number>0 Then

Response.Write err.number& "<font color=red><br>"

Response.Write err.description& "</font>"

End If

Response.Write "<font color=green> backup Successful!</font>"

ElseIf act = "Restore" Then

' Restore when you are not using the database!

Set srv=server.createobject ("SQLDMO. SQL Server ")

Srv. LoginTimeout = Sqllogintimeout

Srv. Connect Sqlserver,sqlname, SQLPassword

Set rest=server.createobject ("SQLDMO. Restore ")

Rest. Action=0 ' Full db restore

Rest. Database=databasename

Rest. Devices=files

Rest. Files=bak_file

Rest. Replacedatabase=true ' Force restore over existing database

If Err.number>0 Then

Response.Write err.number& "<font color=red><br>"

Response.Write err.description& "</font>"

End If

Rest. SQLRestore SRV



Response.Write "<font color=green> Restore Success!</font>"

Else

Response.Write "<font color=red> did not select operation </font>"

End If

End If

%>

</BODY>

</HTML>

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.