asp备份SQL数据库

<% 
'调用方法: DBbackup.asp?dbName=数据库名称
'
只能备份IIS所在的服务器SQL数据库(不能备份远程SQL数据库)。
'
Eric 2009.12.7 14:18
const conf_dbhost = "(local)"
const conf_dbuser = "sa" '数据库用户名
const conf_dbpassword = "inchbyinch" '数据库密码
dim conf_dbname
Dim conf_dbSavePath
conf_dbname = trim(request.QueryString("dbName"))
conf_dbSavePath = server.MapPath("./")&"/"&conf_dbname&getDateTimeSeries()&".bak"
If conf_dbname <>"" then
Set connObj = Server.CreateObject("ADODB.Connection")
connObj.Open "driver={SQL Server};server=" & conf_dbhost & ";uid=" & conf_dbuser & ";pwd=" & conf_dbpassword & ";database=" & conf_dbname
Set Rstmp = Server.CreateObject("adodb.recordset")
strSql = "backup database "&conf_dbname&" TO DISK='"&conf_dbSavePath&"'"
'response.write strSql
Rstmp.Open strSql,connObj,1,1
if err Then
response.Write(Err.Description)
response.End()
set Rstmp = nothing
set connObj = Nothing
Else
Call DownloadFile(conf_dbSavePath)
Call delFile(conf_dbSavePath)
End If
Else
response.write "调用方法:DBbackup.asp?dbName=数据库名称"
End If
'得到由时间生成的随机数 20060101221022位随机数
Function getDateTimeSeries()
dim yyyy,mm,dd,h,m,s,MyValue
yyyy = year(now)
mm = right("00"&cstr(month(now)),2)
dd = right("00"&cstr(day(now)),2)
h = right("00"&cstr(hour(now)),2)
m = right("00"&cstr(minute(now)),2)
s = right("00"&cstr(second(now)),2)
Randomize
MyValue = Int((1000 * Rnd) + 1)
getDateTimeSeries = yyyy&mm&dd&h&m&s
End Function
Function DownloadFile(strFilename)
'清空Buffer
Response.Buffer = True
Response.Clear
'创建Stream对象
Set s = Server.CreateObject("ADODB.Stream")
s.Open
'设置流对象为二进制类型
s.Type = 1
on error resume next
'检测文件是否存在
Set fso = Server.CreateObject("Scripting.FileSystemObject")
'If Not fso.FileExists(strFilename) Then
'
downloadFile="NoFile"
'
Exit Function
'
End If
'
计算文件长度
Set f = fso.GetFile(strFilename)
intFilelength = f.size
If filename="" Then
filename=f.name
End If
s.LoadFromFile(strFilename)
if err then
Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
Response.End
end if
'向用户浏览器发送Header
Response.AddHeader "Content-Disposition", "attachment; filename=" & filename
Response.AddHeader "Content-Length", intFilelength
Response.CharSet = "UTF-8"
Response.ContentType = "application/octet-stream"
'输出文件
'
对于小于4096KB的文件可以用语句
'
Response.BinaryWrite s.Read
'
Response.Flush
'
完成,但对于大于4096KB的文件要分段输出,如下循环操作。
Do While Not s.EOS
Contents = s.Read (4096) '每次读取4096KB
Response.BinaryWrite Contents
Response.Flush
Loop
'清理
s.Close
Set s = Nothing
End Function
'删除文件
Function delFile(fileName)
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fso.GetFile(fileName).Delete()
Set fso = nothing
End Function
%>


 

posted @ 2009-12-07 14:27  奶茶爽歪歪  阅读(288)  评论(0编辑  收藏  举报