ASP backup SQL Server database

Source: Internet
Author: User
<%
'Call method: dbbackup. asp? Dbname = Database Name
'Only the SQL database of the server where IIS is located can be backed up (Remote SQL database cannot be backed up ).
'Eric 2009.12.7
Const conf_dbhost = "(local )"
Const conf_dbuser = "sa" 'database username
Const conf_dbpassword = "inchbyinch" 'Database Password
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 "Call method: dbbackup. asp? Dbname = database name"
End if
'The random number generated by time is 20060101221022-bit.
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)
'Clear the buffer
Response. Buffer = true
Response. Clear
'Create Stream Object
Set S = server. Createobject ("ADODB. Stream ")
S. Open
'Set Stream object to binary type
S. type = 1
On Error resume next
'Check whether the file exists
Set FSO = server. Createobject ("scripting. FileSystemObject ")
'If not FSO. fileexists (strfilename) then
'Downloadfile = "nofile"
'Exit Function
'End if
'Calculate the file length
Set F = FSO. GetFile (strfilename)
Intfilelength = f. Size
If filename = "" then
Filename = f. Name
End if
S. loadfromfile (strfilename)
If err then
Response. Write ("Response. End
End if
'Send the header to the user's browser
Response. addheader "content-disposition", "attachment; filename =" & filename
Response. addheader "Content-Length", intfilelength
Response. charset = "UTF-8"
Response. contenttype = "application/octet-stream"
'Output File
'The statement can be used for files smaller than 4096kb
'Response. binarywrite S. Read
'Response. Flush
'Finished, but for files larger than 4096kb, it must be output in segments. The following is a loop operation.
Do while not S. Eos
Contents = S. Read (4096) '40 96 kb each read
Response. binarywrite Contents
Response. Flush
Loop
'Cleanup
S. Close
Set S = nothing
End Function
'Delete an object
Function delfile (filename)
Set FSO = server. Createobject ("scripting. FileSystemObject ")
FSO. GetFile (filename). Delete ()
Set FSO = nothing
End Function
%>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.