各位高人:
    我想用用VB程序怎样还原数据库:我的代码是如下:
 
Private Sub mnurestore_Click()
On Error GoTo restoreerr
    Dim strfile As String
    Dim strfilename As String
    Dim i As Double
  
  
  CommonDialog1.DialogTitle = "select a file to restore"
  CommonDialog1.ShowOpen
  strfile = CommonDialog1.FileName
  If Trim(strfile) = "" Then Exit Sub  
  For i = 1 To Len(strfile)
    If mId(Right(strfile, i), 1, 1) = "\" Then
      strfilename = Right(strfile, i - 1)
      Exit For
    End If
    
  Next i  
  Dim strsql2 As String
  Dim cmdrestore As ADODB.Command  strsql2 = "RESTORE DATABASE feesys FROM " & strfilename '& '" WITH restricted_user,NORECOVERY"
  gAdoConnect.Close
  Set gAdoConnect = Nothing
  
  Dim cn As ADODB.Connection
  Set cn = New ADODB.Connection
  cn.Open "dsn=feesysdb;uid=sa;pwd=;"
  Set cmdrestore = New ADODB.Command
  
  Set cmdrestore.ActiveConnection = cn  cmdrestore.CommandText = strsql2
  cmdrestore.Execute
    MsgBox "恢复成功!"
  ConnectDB
  Exit Sub但是 老提示如下错误:
     数据库正在使用,所以未能获得数据库的排它访问权 
 诚恳各位相助!!!!!!!!!!!!!!!!

解决方案 »

  1.   

    Dim oldMdb As String
      Dim newMdb As String
                Dim SHFileOp As SHFILEOPSTRUCT            SHFileOp.wFunc = FO_COPY
                SHFileOp.pFrom = App.Path & "\back\msDATAformobile.mdb"
                SHFileOp.pTo = App.Path & "\msDATAformobile.mdb"
                SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR
                Call SHFileOperation(SHFileOp)            MsgBox "复原已完成!"    Exit Sub
      

  2.   

    Dim i As Integer
        i = MsgBox("恢复数据库操作将覆盖现有的数据,是否恢复?", 4 + 32)
        If i = 7 Then Exit Sub
        If Dir(strfilename) = "" Then
            MsgBox "备份文件<" & strfilename & ">不存在,请重新输入!": Exit Sub
        End If
        Dim oSvr As SQLDMO.SQLServer
        On Error Resume Next
        Set oSQLServer = CreateObject("SQLDMO.SQLServer")
        If Err > 0 Then
            If Err = 429 Then
                MsgBox "请首先安装 MSDE": Exit Sub
            End If
        End If
        oSQLServer.LoginSecure = False
        oSQLServer.Connect "dsn=feesysdb;uid=sa;pwd=;"
        If Err <> 0 Then
            MsgBox "连接 SQLServer 不成功,请首先登录 SQLServer": Exit Sub
        End If
        Err = 0
        Set stos = CreateObject("SQLDMO.Restore")
        stos.Action = SQLDMORestore_Database
        stos.Database = "feesysdb"
        stos.ReplaceDatabase = True
        stos.Files = strfilename
    Screen.MousePointer = 11
        DoEvents
        On Error Resume Next
        stos.SQLRestore oSQLServer
        On Error GoTo 0
        Screen.MousePointer = 0
        DoEvents
        Set stos = Nothing
    MsgBox "数据库恢复完成"
      

  3.   

    高手:
        我的代码在SQLSERVER 7.0上好使 但在数据库SQLSERVER 2000 上不好使 你的代码在
    Dim oSvr As SQLDMO.SQLServer 各块出错 无法定义
      

  4.   

    http://www.ourfly.com/forum/View.aspx?fbId=9&Id=146
      

  5.   

    在引用里面添加
     microsoft SQLDMO object library
      

  6.   

    Dim oSvr As SQLDMO.SQLServer各块出错,是因为你没在此工程中引用这个类库.
    在引用中添加microsoft SQLDMO object library就可以了.