ASP备份SQL Server数据库改进版

时间:2022-02-07 01:25:54

代码如下:


<form method="post" name="myform" action="?action=backupdatabase"> 
选择操作:<INPUT TYPE="radio" NAME="act" id="act_backup"value="backup"><label for=act_backup>备份</label> 
<INPUT TYPE="radio" NAME="act" id="act_restore" value="restore"><label for=act_restore>恢复</label> 
<br>数据库名:<INPUT TYPE="text" NAME="databasename" value="<%=request("databasename")%>"> 
<br>文件路径:<INPUT TYPE="text" NAME="bak_file" value="c:\$1.bak">(备份或恢复的文件路径)<br> 
<input type="submit" value="确定" id=submit1 name=submit1> 
</form> 
<% 
'SQL Server 数据库的备份与恢复! 
if request("action")="backupdatabase" Then 
dim sqlserver,sqlname,sqlpassword,sqlLoginTimeout,databasename,bak_file,act 
sqlserver = "localhost" 
sqlname = "sa" 
sqlpassword ="password" 
sqlLoginTimeout = 15 
databasename = trim(request("databasename")) 
bak_file = trim(request("bak_file")) 
bak_file = replace(bak_file,"$1",databasename) 
act = lcase(request("act")) 
if databasename = "" then 
response.write "input database name" 
else 
if act = "backup" then 
Set srv=Server.CreateObject("SQLDMO.SQLServer") 
srv.LoginTimeout = sqlLoginTimeout 
srv.Connect sqlserver,sqlname, sqlpassword 
Set bak = Server.CreateObject("SQLDMO.Backup") 
bak.Database=databasename 
bak.Devices=Files 
bak.Action = 0 
bak.Initialize = 1 
'bak.Replace = True 
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>备份成功!</font>" 
elseif act="restore" then 
'恢复时要在没有使用数据库时进行! 
Set srv=Server.CreateObject("SQLDMO.SQLServer") 
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>恢复成功!</font>" 
else 
Response.write "<font color=red>没有选择操作</font>" 
end if 
end if 
end if 
%>