用Asp备份与恢复SQL Server 数据库
2018-09-06 12:48
一个用asp备份与恢复SQLServer数据库的例子代码,希望对一些朋友有用。(注意:数据库使用中可能无法恢复)
复制代码 代码如下:
<HTML>
<HEAD>
<TITLE>SQLServer数据库的备份与恢复</TITLE>
<metahttp-equiv=Content-Typecontent=text/html;charset=gb2312>
</HEAD>
<BODY>
<formmethod=postname=myform>
选择操作:<INPUTTYPE=radioNAME=actid=act_backupvalue=backup><labelfor=act_backup>备份</label>
<INPUTTYPE=radioNAME=actid=act_restorevalue=restore><labelfor=act_restore>恢复</label>
<br>数据库名:<INPUTTYPE=textNAME=databasenamevalue=<%=request(databasename)%>>
<br>文件路径:<INPUTTYPE=textNAME=bak_filevalue=c:\$1.bak>(备份或恢复的文件路径)<br>
<inputtype=submitvalue=确定>
</form>
<%
SQLServer数据库的备份与恢复!
海娃
dimsqlserver,sqlname,sqlpassword,sqlLoginTimeout,databasename,bak_file,act
sqlserver=localhostsql服务器
sqlname=sa用户名
sqlpassword=sa密码
sqlLoginTimeout=15登陆超时
databasename=trim(request(databasename))
bak_file=trim(request(bak_file))
bak_file=replace(bak_file,$1,databasename)
act=lcase(request(act))
ifdatabasename=then
response.writeinputdatabasename
else
ifact=backupthen
Setsrv=Server.CreateObject(SQLDMO.SQLServer)
srv.LoginTimeout=sqlLoginTimeout
Setbak=Server.CreateObject(SQLDMO.Backup)
bak.Database=databasename
bak.Devices=Files
bak.Files=bak_file
bak.SQLBackupsrv
iferr.number>0then
response.writeerr.number&<fontcolor=red><br>
response.writeerr.description&</font>
endif
Response.write<fontcolor=green>备份成功!</font>
elseifact=restorethen
恢复时要在没有使用数据库时进行!
Setsrv=Server.CreateObject(SQLDMO.SQLServer)
srv.LoginTimeout=sqlLoginTimeout
srv.Connectsqlserver,sqlname,sqlpassword
Setrest=Server.CreateObject(SQLDMO.Restore)
rest.Action=0fulldbrestore
rest.Database=databasename
rest.Devices=Files
rest.Files=bak_file
rest.ReplaceDatabase=TrueForcerestoreoverexistingdatabase
iferr.number>0then
response.writeerr.number&<fontcolor=red><br>
response.writeerr.description&</font>
endif
rest.SQLRestoresrv
Response.write<fontcolor=green>恢复成功!</font>
else
Response.write<fontcolor=red>没有选择操作</font>
endif
endif
%>
</BODY>
</HTML>
文章标题:用Asp备份与恢复SQL Server 数据库
文章链接:http://soscw.com/index.php/essay/10699.html