Dim StrCnn As New Connection
Dim itmX As ListItem
Dim key, list, sql As StringPrivate Sub Command2_Click()
    End
End SubPrivate Sub Form_Load()
  Option1.Value = True
  Dir1_Change
End Sub
Private Sub Dir1_Change()
  File1.Path = Dir1.Path
  ListView1.ListItems.Clear
  If File1.ListCount <> 0 Then
    a = 0
    Do While File1.ListIndex < File1.ListCount - 1
    File1.ListIndex = a
    key = File1.FileName
    Set itmX = ListView1.ListItems.Add(, , key, 1)
    a = a + 1
    Loop
    End If
    File1_click
End Sub
Private Sub Drive1_change()
  Dir1.Path = Drive1.Drive
End Sub
Private Sub File1_click()
  Label2.Caption = Dir1.Path & "\" & File1.FileName
End Sub
Private Sub ListView1_Click()
  Label2.Caption = Dir1.Path & "\" & ListView1.SelectedItem
End Sub
Private Sub Command1_Click()
   Dim s
   If Option1.Value = True Then
       s = Dir1.Path & "\" & Date & ".BAK"
       If Label2.Caption = s Then
          MsgBox " 数据备份卡已存在!"
       Else
      StrCnn.Open "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=Backup "
      sql = "backup DATABASE FDCPGGL TO disk='" & s & "'"
      StrCnn.Execute (sql)                                                  ’调试指向这句
      StrCnn.Close
      MsgBox "数据库备份成功!"
      key = Date & ".BAK"
      Set itmX = ListView1.ListItems.Add(, , key, 1)
      End If
   End If
  If Option2.Value = True Then
     If File1.ListCount <> 0 Then
       If Label2.Caption <> "" Then
          StrCnn.Open "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=Backup "
          sql = "RESTORE DATABASE FDCPGGL FROM disk='" & Label2.Caption & "'"
          StrCnn.Execute (sql)
          StrCnn.Close
           MsgBox "数据库恢复成功!"
       Else
           MsgBox "请选择要恢复的数据备份卡!"
       End If
     Else
          MsgBox "请选择要恢复的数据备份卡!"
     End If
   End If
End Sub

解决方案 »

  1.   

    文件
    Dir1.Path & "\" & Date & ".BAK"
    不存在
      

  2.   

    A)BACKUP DATABASE 是在服务端执行的,你本机的路径是无效的。
    B)就算数据库运行在本机,它的启动用户和你当前用户是不一样的,访问目录还存在权限问题,最好把目录权设为 everyone 完全控制。
      

  3.   

                   s = Dir1.Path & "\" & "房地产评估管理系统数据库" & ".mdf"
    我将上面这个地方改了后,备份成功了。
    但恢复又不对,提示restore无法处理数据库‘FDCPGGL’,因为它正由此会话使用。建议执行此操作时使用master数据库。我就将这句改为          sql = "RESTORE DATABASE master  FROM disk='" & Label2.Caption & "'"
    但提示若要还原master数据库,服务器必须以单用户模式运行。
      

  4.   

    '你配置的这个 Backup 数据源中,默认数据库不要设为 FDCPGGL,而是要用其它数据库(比如 master)'
    StrCnn.Open "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=Backup"
    '否则连接后 FDCPGGL 正在使用,是不能做恢复的'
    sql = "RESTORE DATABASE FDCPGGL FROM disk='" & Label2.Caption & "'"