网络编程
位置:首页>> 网络编程>> Asp编程>> asp 在线备份与恢复sql server数据库的代码

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

  发布时间:2010-07-31 18:52:00 

标签:sql,server,数据库,备份,恢复

asp 在线备份 恢复 sql server 数据库,对于远程没有提供sql server远程连接或打包下载的朋友是个临时解决方法,对于大数据来说可能效果不好。

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、恢复sql server

<% 
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 
%> 

 

0
投稿

猜你喜欢

  • 无论安装何版本的mysql,在管理工具的服务中启动mysql服务时都会在中途报错。内容为:在 本地计算机 无法启动mysql服务 错误106
  • 分享人:轻侯设计师常有这样的疑惑:如何知道用户浏览网页的习惯?如何设计出符合用户使用习惯的网页?如何从搜索引擎带来更多的流量?眼动研究可以帮
  • 这句话后面的1200是什么单位来的啊?delete from online where datediff(""s&quo
  • 呵,以前也没考虑过这方面的东西,现在写的代码越来越多,越来越复杂,如果再不把不用的变量及时释放掉,到时肯定会出问题。今天无意中在无忧Q群里看
  • 问题:SQL Server 2000中设计表时如何得到自动编号字段?解答:具体步骤如下:①像Access中的自动编号字段右键你的表-->
  • [原文地址] VS 2008 Performance Improvements[原文发表时间] Thursday, September 27
  • 在浏览器 IE6 、IE7、Firefox2+、Firefpx3+、Opera9.6+、Safari3.1+中测试以下代码:<!DOC
  • 试了一下,xmlDoc.save()行不同,就试着用fso做了出来。整理一下,供大家discuss。由于用js操作本地xml文件之后save
  • 数据库连接:<% set conn=server.createobject("adodb.connection&q
  • 下面继续为大家带来XHTML与HTML兼容的16条指引!1.避免将页面声明为XML类型,页面使用UTF-8或者UTF-16字符集。2.在空元
  • 数据库是什么 在学习ACCESS之前,我们先了解一下什么是“数据库”。我们举个例子来说明这个问题:每个人都有很多亲戚和朋友,为了保持与他们的
  • 以发布目录为例:<OBJECT ID="agobjOraSession" RUNAT=&quo
  • 工作中,网页设计师经常会遇见这些状况:时间这么短又要出彩、又是要大气要有气氛、风格不明确很难把握、栏目这么多页面又这么长……突然觉得束手无策
  • 前几天翻出以前写的一个纯CSS仿微软经典菜单,现在看来才感叹,微软的经典菜单确实很经典,至少看起来不觉得厌烦。感叹归感叹,想想既然可以实现下
  • 常规循环引用内存泄漏和Closure内存泄漏 要了解javascript的内存泄漏问题,首先要了解的就是javascript的GC原理。我记
  • 一个网站空间,但是却可以实现多个域名的访问的一段ASP代码:<%if Request.ServerVariables("SE
  • 加号+,  是字符串优先.并且从左向右计算. 就是运算前后两个值,只要有一个是字符串,就会将其中一个非字符串的试图转换成字符串.
  • <%  Response.Buffer = True  Server.ScriptTimeOut=9999999&
  • 下面这些命令可以在命令行下用isql执行,isql -E -Q "命令",isql.exe
  • 随着网络技术的不断发展,网络应用已经渗透到人类社会的各个角落。作为网络世界的支撑点的网站,更是人们关注的热点:政府利用网站宣传自己的施政纲领
手机版 网络编程 asp之家 www.aspxhome.com