ASP實現備份sql server資料

來源:互聯網
上載者:User
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>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.