asp 線上備份與恢複sqlserver資料庫的代碼

來源:互聯網
上載者:User

asp線上備份sql server資料庫:
1、備份sqlserver 複製代碼 代碼如下:<%
SQL="backup database 資料庫名 to disk='"&Server.MapPath("backup")&"\"&"backuptext.dat"&"'"
set cnn=Server.createobject("adodb.connection")
cnn.open "driver={SQL Server};Server=伺服器名;uid=sa;pwd="
cnn.execute SQL
on error resume next
if err<>0 then
response.wrITe "錯誤:"&err.Descripting
else
response.wrITe "資料備份成功!"
end if
%>

2、恢複sqlserver 複製代碼 代碼如下:<%
SQL="Restore database 資料庫名 from disk='"&Server.MapPath("backup")&"\"&"backuptext.dat"&"'"
set cnn=Server.createobject("adodb.connection")
cnn.open "driver={SQL Server};Server=伺服器名;uid=sa;pwd="
cnn.execute SQL
on error resume next
if err<>0 then
response.wrITe "錯誤:"&err.Descripting
else
response.wrITe "資料恢複成功!"
end if
%>

ACCESS原理一樣 複製代碼 代碼如下:<%
'*****************************************
function CopyTo(ByVal cFile,ByVal toFile)
cFile=Server.MapPath(cFile) ‘所要備份的檔案
toFile=Server.MapPath(toFile) ‘備份檔案
Dim cFso,cf
set cFso=Server.CreateObject("Scripting.FileSystemObject")
cFso.fileexists(cFile)
cFso.Copyfile cFile,toFile
end function
'*********************************************
' ASP實現備份及恢複ACCESS資料庫操作
'本頁面為 databackup.asp
dim dbpath,bkfolder,bkdbname,fso,fso1
call main()
call main2()
conn.close
set conn=nothing
sub main()
if request("action")="Backup" then
call backupdata()
else
%>
<table cellspacing=1 cellpadding=1 align=center width="90%">
<tr>
<th height=25 >
<B>Database Backup</B>
</th>
</tr>
<form method="post" action="databackup.asp?action=Backup">
<tr>
<td height=100 style="line-height:150%">

當前資料庫路徑(相對路徑):
<input type=text size=15 name=DBpath value="../mdb/database.mdb"><BR>
備份資料庫目錄(相對路徑):
<input type=text size=15 name=bkfolder value=../Databackup> 如目錄不存在,程式將自動建立<BR>
備份資料庫名稱(填寫名稱):
<input type=text size=15 name=bkDBname value=database.mdb> 如備份目錄有該

檔案,將覆蓋,如沒有,將自動建立<BR>
<input type=submIT value="備份資料"><hr align="center" width="90%" color="#999999"></td>
</tr>
</form>
</table>
<%
end if
end sub
sub main2()
if request("action")="Restore" then
Dbpath=request.form("Dbpath")
backpath=request.form("backpath")
if dbpath="" then
response.wrITe "請輸入您要恢複成的資料庫全名"
else
Dbpath=server.mappath(Dbpath)
end if
backpath=server.mappath(backpath)
Response.wrITe Backpath
Set Fso=server.createobject("scripting.filesystemobject")
if fso.fileexists(dbpath) then
fso.copyfile Dbpath,Backpath
response.wrITe "<font color=red>成功恢複資料!</font>"
else
response.wrITe "<font color=red>備份目錄下並無您的備份檔案!</font>"
end if
else
%>
<table align=center cellspacing=1 cellpadding=1 width="90%">
<tr>
<th height=25 >
<B>恢複資料庫</B>
</th>
</tr>
<form method="post" action="databackup.asp?action=Restore">
<tr>
<td height=100 >
備份資料庫路徑(相對):
<input type=text size=30 name=DBpath value="../Databackup/database.mdb"> <BR>
當前資料庫路徑(相對):
<input type=text size=30 name=backpath value="../mdb/database.mdb"><BR>
<input type=submIT value="恢複資料"> <hr width="90%" align="center" color="#999999">
<font color="#666666">·注意:所有路徑都是相對路徑 </font></td>
</tr>
</form>
</table>
<%
end if
end sub
sub backupdata()
Dbpath=request.form("Dbpath")
Dbpath=server.mappath(Dbpath)
bkfolder=request.form("bkfolder")
bkdbname=request.form("bkdbname")
Set Fso=server.createobject("scripting.filesystemobject")
if fso.fileexists(dbpath) then
If CheckDir(bkfolder) = True Then
fso.copyfile dbpath,bkfolder& "\\"& bkdbname
else
MakeNewsDir bkfolder
fso.copyfile dbpath,bkfolder& "\\"& bkdbname
end if
response.wrITe "<font color=red>備份資料庫成功,您備份的資料庫路徑為" &bkfolder& "\\"& bkdbname+"</font>"
Else
response.wrITe "<font color=red>找不到您所需要備份的檔案。</font>"
End if
end sub
'------------------檢查某一目錄是否存在-------------------
Function CheckDir(FolderPath)
folderpath=Server.MapPath(".")&"\\"&folderpath
Set fso1 = CreateObject("Scripting.FileSystemObject")
If fso1.FolderExists(FolderPath) then
'存在
CheckDir = True
Else
'不存在
CheckDir = False
End if
Set fso1 = nothing
End Function
'-------------根據指定名稱組建目錄---------
Function MakeNewsDir(foldername)
dim f
Set fso1 = CreateObject("Scripting.FileSystemObject")
Set f = fso1.CreateFolder(foldername)
MakeNewsDir = True
Set fso1 = nothing
End Function
%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.