server|sql|備份|資料
建個bak檔案夾啊,放資料!
db.asp代碼如下:
<%
dim conn
dim connstr
'on error resume next
set conn=server.CreateObject("adodb.connection")
connstr="Provider=SQLOLEDB;data source=192.168.2.1;UID=sa;Pwd=123;DataBase=test"
conn.Open connstr
if err<>0 then
Response.Write "無法建立到資料庫的串連!"
end if
%>
backup.asp代碼如下:
<!--#include file="db.asp"-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META NAME="Generator" CONTENT="EditPlus">
<TITLE> 資料備份系統 </TITLE>
<style type="text/css">
a{color:fff;text-decoration:none;}
a:hover{color:ccc;text-decoration:underline;}
ul{border-top:1px solid #fff;
border-left:1px solid #fff;
border-right:1px solid #fff;
margin:0px;
width:400px;}
li{list-style:none;border-bottom:1px solid #fff;height:25px;padding:4px;text-align:left;}
#box{list-style:none;border-bottom:1px solid #fff;height:25px;padding:4px;text-align:center;}
body{
background:#037EC5;
color:#fff;
font-size:12px;
text-align:center;
font-family:tahoma;
margin:0px;
}
#bak{position:absolute;
left:360px;
top:100px;
height:40px;
border:1px solid #ccc;
padding:20px;
FILTER: Alpha(opacity=75);
background:#fff;
color:#000;
}
</style>
</HEAD>
<BODY>
<%
Server.ScriptTimeOut=1000
ON error resume next
'====================
'=====xhtml製作=========
'=====2005-3-31======
function CreateFile(sql)
Dim fso, tf,filename
filename=replace(replace(now()," ","-"),":","-")&".sql"
filename=server.mappath("bak/"&table&filename)
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile(filename, True)
tf.WriteLine "SET IDENTITY_INSERT ["&table&"] ON"
tf.WriteLine sql
tf.WriteLine "SET IDENTITY_INSERT ["&table&"] OFF"
tf.Close
end function
'讀取表名
Set rs = conn.OpenSchema(20)
Response.Write"<ul><li id='box'>SQL SERVER資料備份指令碼</li>"
Do While Not rs.EOF
If rs("TABLE_TYPE") = "TABLE" Then
Response.Write "<li>□ <a href='?table="&rs("TABLE_NAME")&"'>備份 "& rs("TABLE_NAME") &"</a></li>"
End If
rs.MoveNext
Loop
Response.Write"</ul>"
Set rs = Nothing
table=request.querystring("table")
if table<>"" then
mysql="select * from "&table
set rs=server.createobject("adodb.recordset")
rs.open mysql,conn,1,1
' 將欄位名稱迴圈讀出
ColCount = Rs.Fields.Count
for intCount= 0 to ColCount-2
sqlBak=sqlBak&rs.Fields(intCount).Name&","
next
'在最後的欄位後加括弧
sqlBak=sqlBak&Rs.Fields(ColCount-1).Name&")"
sqlHead="INSERT into ["&table&"]("&sqlBak&"values("
do while not rs.eof
for intCount= 0 to ColCount-2
mainSTR=replace(Rs.Fields(intCount).Value,"'","''")
if Rs.Fields(intCount).type=3 then
sqlData=sqlData&mainSTR&","
else
sqlData=sqlData&"'"&mainSTR&"',"
end if
next
'最後的欄位
'替換單引號
lastSTR=replace(Rs.Fields(ColCount-1).Value,"'","''")
if Rs.Fields(ColCount-1).type=3 then
sqlData=sqlData&lastSTR&")"&vblf&sqlHead
else
sqlData=sqlData&"'"&lastSTR&"')"&vblf&sqlHead
end if
rs.movenext
loop
sqldata=sqlHead&sqldata
sqllong=len(sqldata)-len(vblf&sqlHead)
sqldata=left(sqldata,sqllong)
rs.close
set rs=nothing
conn.close
set conn=nothing
CreateFile(sqldata)
else
response.end
end if
%>
</BODY>
</HTML>