»áÔ±£º ÃÜÂ룺 ¡¡Ãâ·Ñ×¢²á | Íü¼ÇÃÜÂë | »áÔ±µÇ¼ ÍøÒ³¹¦ÄÜ£º ¼ÓÈëÊÕ²Ø ÉèΪÊ×Ò³ ÍøÕ¾ËÑË÷  
¼¼ÊõÎĵµ > VBÎĵµ > Êý¾Ý±¨±í
VBʵÏÖSQL ServerÊý¾Ý¿â±¸·Ý/»Ö¸´
·¢±íÈÕÆÚ£º2005-02-25 16:39:55×÷Õߣº× ³ö´¦£º  

'*************************************************************************
'**Ä£ ¿é Ãû£ºfBackupDatabase_a
'**Ãè    Êö£º±¸·ÝÊý¾Ý¿â,·µ»Ø³ö´íÐÅÏ¢,Õý³£»Ö¸´,·µ»Ø""
'**µ÷    ÓãºfBackupDatabase_a "±¸·ÝÎļþÃû","Êý¾Ý¿âÃû"
'**²ÎÊý˵Ã÷£º
'**          sBackUpfileName  »Ö¸´ºóµÄÊý¾Ý¿â´æ·ÅĿ¼
'**          sDataBaseName    ±¸·ÝµÄÊý¾ÝÃû
'**          sIsAddBackup     ÊÇ·ñ×·¼Óµ½±¸·ÝÎļþÖÐ
'**˵    Ã÷£ºÒýÓÃMicrosoft ActiveX Data Objects 2.x Library
'**´´ ½¨ ÈË£º×Þ½¨
'**ÈÕ    ÆÚ£º2003Äê12ÔÂ09ÈÕ
'*************************************************************************
Public Function fBackupDatabase_a(ByVal sBackUpfileName$ _
                                , ByVal sDataBaseName$ _
                                , Optional ByVal sIsAddBackup As Boolean = False _
                                ) As String
                                
    Dim iDb As ADODB.Connection
    Dim iConcStr$, iSql$, iReturn$
    
    On Error GoTo lbErr
    
    '´´½¨¶ÔÏó
    Set iDb = New ADODB.Connection
    
    'Á¬½ÓÊý¾Ý¿â·þÎñÆ÷,¸ù¾ÝÄãµÄÇé¿öÐÞ¸ÄÁ¬½Ó×Ö·û´®
    iConcStr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=zj"
    iDb.Open iConcStr
    
    'Éú³ÉÊý¾Ý¿â±¸·ÝÓï¾ä
    iSql = "backup database [" & sDataBaseName & "]" & vbCrLf & _
            "to disk='" & sBackUpfileName & "'" & vbCrLf & _
            "with description='" & "zj-backup at:" & Date & "(" & Time & ")'" & vbCrLf & _
            IIf(sIsAddBackup, "", ",init")
            
    iDb.Execute iSql
    GoTo lbExit
    
lbErr:
    iReturn = Error
lbExit:
    fBackupDatabase_a = iReturn
End Function

'*************************************************************************
'**Ä£ ¿é Ãû£ºfrestoredatabase_a
'**Ãè    Êö£º»Ö¸´Êý¾Ý¿â,·µ»Ø³ö´íÐÅÏ¢,Õý³£»Ö¸´,·µ»Ø""
'**µ÷    Óãºfrestoredatabase_a "±¸·ÝÎļþÃû","Êý¾Ý¿âÃû"
'**²ÎÊý˵Ã÷£º
'**          sDataBasePath  »Ö¸´ºóµÄÊý¾Ý¿â´æ·ÅĿ¼
'**          sBackupNumber  ÊÇ´ÓÄǸö±¸·ÝºÅ»Ö¸´
'**          sReplaceExist  Ö¸¶¨ÊÇ·ñ¸²¸ÇÒѾ­´æÔÚµÄÊý¾Ý
'**˵    Ã÷£ºÒýÓÃMicrosoft ActiveX Data Objects 2.x Library
'**´´ ½¨ ÈË£º×Þ½¨
'**ÈÕ    ÆÚ£º2003Äê12ÔÂ09ÈÕ
'*************************************************************************
Public Function fRestoreDatabase_a(ByVal sBackUpfileName$ _
                                , ByVal sDataBaseName$ _
                                , Optional ByVal sDataBasePath$ = "" _
                                , Optional ByVal sBackupNumber& = 1 _
                                , Optional ByVal sReplaceExist As Boolean = False _
                                ) As String
    
    Dim iDb As ADODB.Connection, iRe As ADODB.Recordset
    Dim iConcStr$, iSql$, iReturn$, iI&
    
    On Error GoTo lbErr
    
    '´´½¨¶ÔÏó
    Set iDb = New ADODB.Connection
    Set iRe = New ADODB.Recordset
    
    'Á¬½ÓÊý¾Ý¿â·þÎñÆ÷,¸ù¾ÝÄãµÄÇé¿öÐÞ¸ÄÁ¬½Ó×Ö·û´®
    iConcStr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=zj"
    iDb.Open iConcStr
    
    'µÃµ½»¹Ô­ºóµÄÊý¾Ý¿â´æ·ÅĿ¼,Èç¹ûûÓÐÖ¸¶¨,´æ·Åµ½SQL SERVERµÄDATAĿ¼
    If sDataBasePath = "" Then
        iSql = "select filename from master..sysfiles"
        iRe.Open iSql, iDb, adOpenKeyset, adLockReadOnly
        iSql = iRe(0)
        iRe.Close
        sDataBasePath = Left(iSql, InStrRev(iSql, "\"))
    End If
    
    '¼ì²éÊý¾Ý¿âÊÇ·ñ´æÔÚ
    If sReplaceExist = False Then
        iSql = "select 1 from master..sysdatabases  where name='" & sDataBaseName & "'"
        iRe.Open iSql, iDb, adOpenKeyset, adLockReadOnly
        If iRe.EOF = False Then
            iReturn = "Êý¾Ý¿âÒѾ­´æÔÚ!"
            iRe.Close
            GoTo lbExit
        End If
        iRe.Close
    End If
    
    '¹Ø±ÕÓû§½ø³Ì,·ÀÖ¹ÆäËüÓû§ÕýÔÚʹÓÃÊý¾Ý¿â,µ¼ÖÂÊý¾Ý»Ö¸´Ê§°Ü
    iSql = "select spid from master..sysprocesses where dbid=db_id('" & sDataBaseName & "')"
    iRe.Open iSql, iDb, adOpenKeyset, adLockReadOnly
    While iRe.EOF = False
        iSql = "kill " & iRe(0)
        iDb.Execute iSql
        iRe.MoveNext
    Wend
    iRe.Close
    
    '»ñÈ¡Êý¾Ý¿â»Ö¸´ÐÅÏ¢
    iSql = "restore filelistonly from disk='" & sBackUpfileName & "'" & vbCrLf & _
        "with file=" & sBackupNumber
    iRe.Open iSql, iDb, adOpenKeyset, adLockReadOnly
    
    'Éú³ÉÊý¾Ý¿â»Ö¸´Óï¾ä
    iSql = "restore database [" & sDataBaseName & "]" & vbCrLf & _
        "from disk='" & sBackUpfileName & "'" & vbCrLf & _
        "with file=" & sBackupNumber & vbCrLf
    With iRe
        While Not .EOF
            iReturn = iRe("PhysicalName")
            iI = InStrRev(iReturn, ".")
            iReturn = IIf(iI = 0, "", Mid(iReturn, iI)) & "'"
            iSql = iSql & ",move '" & iRe("LogicalName") & _
                    "' to '" & sDataBasePath & sDataBaseName & iReturn & vbCrLf
            .MoveNext
        Wend
        .Close
    End With
    iSql = iSql & IIf(sReplaceExist, ",replace", "")
    
    iDb.Execute iSql
    iReturn = ""
    GoTo lbExit
    
lbErr:
    iReturn = Error
lbExit:
    fRestoreDatabase_a = iReturn
End Function
 

 

¡¾·µ»Ø¶¥²¿¡¿ ¡¾´òÓ¡±¾Ò³¡¿ ¡¾¹Ø±Õ´°¿Ú¡¿

¹ØÓÚÎÒÃÇ / ¸øÎÒÁôÑÔ / °æȨ¾Ù±¨ / Òâ¼û½¨Òé / ÍøÕ¾±à³ÌQQȺ   
Copyright ©2003- 2024 Lihuasoft.net webmaster(at)lihuasoft.net ¼ÓÔØʱ¼ä 0.00407