用Asp备份与恢复SQL Server 数据库

时间:2021-05-18

一个用asp备份与恢复SQLServer数据库的例子代码,希望对一些朋友有用。(注意:数据库使用中可能无法恢复)
复制代码 代码如下:
<HTML>
<HEAD>
<TITLE>SQLServer数据库的备份与恢复</TITLE>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
</HEAD>
<BODY>
<formmethod="post"name=myform>
选择操作:<INPUTTYPE="radio"NAME="act"id="act_backup"value="backup"><labelfor=act_backup>备份</label> 

<INPUTTYPE="radio"NAME="act"id="act_restore"value="restore"><labelfor=act_restore>恢复</label>

<br>数据库名:<INPUTTYPE="text"NAME="databasename"value="<%=request("databasename")%>">

<br>文件路径:<INPUTTYPE="text"NAME="bak_file"value="c:\$1.bak">(备份或恢复的文件路径)<br>

<inputtype="submit"value="确定">

</form>

<%

'SQLServer数据库的备份与恢复!

'海娃

'http://www.51windows.Net

dimsqlserver,sqlname,sqlpassword,sqlLoginTimeout,databasename,bak_file,act

sqlserver="localhost"'sql服务器

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.write"inputdatabasename"

else

ifact="backup"then

Setsrv=Server.CreateObject("SQLDMO.SQLServer")

srv.LoginTimeout=sqlLoginTimeout

srv.Connectsqlserver,sqlname,sqlpassword

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="restore"then

'恢复时要在没有使用数据库时进行!

Setsrv=Server.CreateObject("SQLDMO.SQLServer")

srv.LoginTimeout=sqlLoginTimeout

srv.Connectsqlserver,sqlname,sqlpassword

Setrest=Server.CreateObject("SQLDMO.Restore")

rest.Action=0'fulldbrestore

rest.Database=databasename

rest.Devices=Files

rest.Files=bak_file

rest.ReplaceDatabase=True'Forcerestoreoverexistingdatabase

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>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章