欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp 在线备份与恢复sqlserver数据库的代码

程序员文章站 2023-01-24 21:10:43
asp在线备份sql server数据库: 1、备份sqlserver 复制代码 代码如下: <% sql="backup database 数据库名 to disk...
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>数据库备份</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
%>