'请参考:
Private Sub backup_Click()
On Error Resume NextWith dlg
     .CancelError = True
     .DialogTitle = "备份数据库到 ..."
     .Flags = cdlOFNOverwritePrompt
     .InitDir = "\\" & g_SERVER
     .Filter = ""
     .ShowSave
     If err.Number <> 0 Then Exit Sub
     Screen.MousePointer = 11
     DoEvents
     err.Clear
     cn.Execute "EXEC sp_addumpdevice 'disk','gzbkdev','" & .FileName & "'"
     cn.Execute "BACKUP DATABASE GZ TO gzbkdev"
     cn.Execute "EXEC sp_dropdevice gzbkdev"
End With
    If err.Number = 0 Then
    MsgBox "备份数据库完成。", vbOKOnly + vbInformation, "备份"
    Else
     cn.Execute "EXEC sp_dropdevice gzbkdev"
    MsgBox "备份数据库失败!" & Chr(13) & err.Description, vbOKOnly + vbCritical, "失败"
    End If
Screen.MousePointer = 0
End Sub

解决方案 »

  1.   

    cnn.Open "Provider=SQLOLEDB.1;" _
            & "Persist Security Info=False;" _
            & "User ID=aaa;" _
            & "Pwd=a2001;" _
            & "Initial Catalog=master;" _
            & "Data Source=" & mstrServer
        Screen.MousePointer = vbHourglass
    '    staInfo.Panels(1).Text = "正在备份..."
        staInfo.Panels(1).Text = LoadResString(212 & mintLang)
        strSql = "Backup Database Data" & CStr(mintAccNo) & " To Disk='" & strFileName & "' With INIT"
        cnn.Execute strSql
      

  2.   


    恢复
     cnn.Open "Provider=SQLOLEDB.1;" _
                & "Persist Security Info=False;" _
                & "User ID=aserp;" _
                & "Pwd=aserp2001;" _
                & "Initial Catalog=master;Connect Timeout = 0;" _
                & "Data Source=" & Trim(txtDbServer.Text)
        strSql = "Select phyname From sysdevices Where name='master'"
        rstTmp.Open strSql, cnn
        strPath = rstTmp(0) & ""
        rstTmp.Close
        strPath = Left$(strPath, Len(strPath) - 10)
        strDevice = "ErpDbBak"
        strFile = txtbak.text
      strSql = "Select cnt=Count(*) From sysdevices Where name='ErpDbBak'"
        rstTmp.Open strSql, cnn
        If rstTmp!cnt <> 0 Then
            rstTmp.Close
            strSql = "Exec sp_dropdevice 'ErpDbBak',DELFILE"
            cnn.Execute (strSql)
        End If
        strSql = "Exec sp_addumpdevice 'disk', 'ErpDbBak','" & strFile & "'"
        cnn.Execute (strSql)
        strSql = "restore filelistonly from ErpDbBak"
        If rstTmp.State = 1 Then rstTmp.Close
        rstTmp.CursorLocation = adUseClient
        rstTmp.Open strSql, cnn
        If Not rstTmp.EOF Then
            lngI = rstTmp.RecordCount
            If lngI = 2 Then
                strOldNo = rstTmp("logicalname")
                rstTmp.MoveNext
                strOldLog = rstTmp("logicalname")
            Else
               rstTmp.Close
               Set rstTmp = Nothing
               cnn.Close
               Set cnn = Nothing
             '  MsgBox "备份集中的数据库不唯一,不能恢复!", vbInformation, Title
               MsgBox LoadResString(367 & mintLang), vbInformation, Title
               Exit Sub
            End If
        Else
            rstTmp.Close
            Set rstTmp = Nothing
            cnn.Close
            Set cnn = Nothing
          '  MsgBox "备份集格式不对或文件版本不对,不能恢复!", vbInformation, Title
            MsgBox LoadResString(368 & mintLang), vbInformation, Title
            Exit Sub
        End If
        
        DoEvents    strSql = "Restore database " & strNo & " from ErpDbBak" & vbCrLf & " With Replace "
        strSql = strSql & vbCrLf & ",Move '" & strOldNo & "' To '" & strPath & strNo & ".mdf'" _
                     & vbCrLf & ",Move '" & strOldLog & "' To '" & strPath & strNo & "_log.ldf'"
        cnn.Execute strSql
      

  3.   

    远程要指定目录就可能要用到Exec xp_dirtree '" & strDir & "',1,1
    查找目录