通过asp代码去备份sql server数据库,不使用工具,获得数据库备份文件后,通过sql server 管理端恢复数据库
<% on error resume next const SqlDriverType=2 const DatabaseName="databasename" const SqlLocalName="sqlserver_address" const SqlUID="username" const SqlPWD="password" const dbBakSavePath="./backup_db/" dim connStr,db,conn sub OpenConn(relJump) if SqlDriverType=2 then connStr = "Driver={SQL Server};server="&SqlLocalName&";uid="&SqlUID&";pwd="&SqlPWD&";database="&DatabaseName&";" end if set conn=server.createobject("Adodb.Connection") conn.open connStr if err then err.clear Set conn = Nothing Response.Write "Error : Failed connect to the database." Response.End else Response.Write " connect to the database success!!!" end if end sub sub CloseConn() conn.close set conn=nothing end sub ' call OpenConn("") ' call CloseConn("") bakName = year(now)&month(now)&day(now)&hour(now)&Minute(now)&Second(now) bakName=""&dbBakSavePath&bakName&".bak" ' response.write server.MapPath(bakName)&" " ' Set srv=Server.CreateObject("SQLDMO.SQLServer") ' srv.LoginTimeout = 150 ' srv.Connect SqlLocalName, SqlUID, SqlPWD ' Set bak = Server.CreateObject("SQLDMO.Backup") ' bak.Database=DatabaseName ' bak.Devices=Files ' bak.Files=server.MapPath(bakName) ' bak.SQLBackup srv ' if err then ' Response.Write "数据库备份失败"&err.Description&"" ' Response.End ' else ' Response.Write "数据库备份成功,备份文件名称" ' Response.End ' end if SQL="backup database "&DatabaseName&" to disk='"&Server.MapPath(bakName)&"'" set cnn=Server.createobject("adodb.connection") cnn.open "driver={SQL Server};Server="&SqlLocalName&";uid="&SqlUID&";pwd="&SqlPWD&"" cnn.execute SQL on error resume next if err<>0 then response.write "错误:"&err.Descripting else response.write "数据备份成功!" end if %>