看過資料庫的備份與還原。大多數都是用組件來完成的。其實可透過sql語句來完成。
由於時間關係,未對參數進行驗證和介面美化。代碼僅供參考。
共4個檔案:下載位址: function rec(rs,sql)
set rs = server.CreateObject("ADODB.Recordset")
rs.Activeconnection = conn
rs.Source = sql
rs.CursorType = 0
rs.Cursorlocation = 3
rs.LockType = 1
rs.Open
if rs.eof and rs.bof then
rec= false
else
rec= true
end if
end function
function cmd(sql)
dim cmd1
set cmd1 = server.CreateObject("ADODB.Command")
cmd1.ActiveConnection = conn
cmd1.CommandText = sql
cmd1.CommandType = 1
cmd1.CommandTimeout = 0
cmd1.Prepared = true
cmd1.Execute()
end function
function cdb(rs)
rs.close()
set rs=nothing
end function
%>
dev.asp
<!--#include file="conn.asp"-->
<%
if request("AddDev") <> "" then
sql="select name,phyname from master..sysdevices where status=16 and name='"&request("devname")&"'"
rec chk,sql
if chk.eof and chk.bof then
sql = "sp_addumpdevice 'disk','"&request("devname")&"','"&request("phyname")&"'"
cmd sql
response.Write "<script language=javascript>window.location=reload;</script>"
else
response.Write "<script language=javascript>alert('資料庫中已存在"&request("devname")&"裝置!');window.location='cmd.asp';</script>"
end if
end if
if request("deldev") <> "" then
sql = "sp_dropdevice '"&request("devname")&"'"
cmd sql
response.Write "<script language=javascript>window.location=reload;</script>"
end if
rec li,"select name,phyname from master..sysdevices where status=16"
if li.eof and li.bof then
response.Write "<font color=#ff0000>請新備份設備用來備份還原資料庫</font>"
else
%><style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
}
-->
</style>
<table width="60%" border="0" cellspacing="0">
<tr>
<td width="29%">名稱:</td>
<td width="71%">位置:</td>
</tr>
<%
i=0
while not li.eof
i=i+1
%>
<tr>
<td><%=li(0)%></td>
<td><%=li(1)%> <a href='dev.asp?DelDev=1&devname=<%=li(0)%>'>刪除此裝置</a></td>
</tr>
<%
li.movenext
wend
response.Write "<script language=javascript>top.document.all.dev.height='"&(i+1)*25&"';</script>"
cdb li
%>
</table>
<%
end if
%>
default.asp
< %@LANGUAGE="VBSCRIPT " CODEPAGE="936"%>
<!--#include file="conn.asp"-->
<%
'*************************************************
'資料庫備份與還原
'編 程:魔術師楊(MagicYang.CN)
'完成日期:2004-4-11
'說明:由於時間關係,未對資料做安全性驗證。
'應用時請對資料進行驗證,確保安全。
'QQ:1168064 歡迎大家互相交流
'*************************************************
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>資料庫的備份與還原</title>
</head>
<body>
<%
%>
<table width="80%" border="0" align="center">
<tr>
<td width="21%" valign="top">資料庫設備:</td>
<td width="79%">
<iframe name="dev" src="dev.asp" width="100%" height="200" frameborder="0" scrolling="no"></iframe>
</td>
</tr>
<tr>
<td>新增設備:</td>
<td>設備名稱:
<input name="devname" type="text" id="devname" size="10" maxlength="10"></td>
</tr>
<tr>
<td> </td>
<td>檔案路徑:
<input name="phyname" type="text" id="phyname" size="20" maxlength="50">
<input type="button" value="新增裝置" onClick="document.all.dev.src='dev.asp?AddDev=1&devname='+document.all.devname.value+'&phyname='+document.all .phyname.value;"></td>
</tr>
<tr>
<td valign="top">備份/還原:</td>
<td>
資料庫名稱:
<%
rec li,"select name from master..sysdatabases where status=16"
%>
<select name="b_data">
<%
while not li.eof
response.Write "<option value="&li(0)&">"&li(0)&"</option>"
li.movenext
wend
cdb li
%>
</select><br>
備份到設備:<%
rec li,"select name,phyname from master..sysdevices where status=16"
%>
<select name="b_dev">
<%
while not li.eof
response.Write "<option value="&li(0)&">"&li(0)&"(檔案:"&li(1)&")</option>"
li.movenext
wend
cdb li
%>
</select><br>
<input name="按鈕" type="button" value="備份資料庫" onClick="window.location='cmd.asp?action=backup&database='+document.all.b_data.value+'&dev='+document. all.b_dev.value;">
<input name="按鈕" type="button" value="還原資料庫" onClick="window.location='cmd.asp?action=RESTORE&database='+document.all.b_data.value+'&dev='+document. all.b_dev.value;"> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>
cmd.asp
<!--#include file="conn.asp"-->
<%
on error resume next
if request("action") <> "" then
if request("action")="backup" then
sql ="BACKUP DATABASE "&request("database")&" To "& request("dev")
cmd sql
if err.number > 0 then
response.Write "<script language=javascript>alert('資料庫"&request("database")&"備份失敗!');window.location='default.asp';</script>"
else
response.Write "<script language=javascript>alert('資料庫"&request("database")&"備份成功!');window.location='default.asp';</script>"
end if
else
sql ="RESTORE DATABASE "&request("database")&" From "& request("dev")
cmd sql
if err.number > 0 then
response.Write "<script language=javascript>alert('資料庫"&request("database")&"還原失敗!');window.location='default.asp';</script>"
else
response.Write "<script language=javascript>alert('資料庫"&request("database")&"還原成功!');window.location='default.asp';</script>"
end if
end if
end if
%>
希望大家互相交流! QQ:1168064