2012-01-15 7 views
-2

エラー:"データベースのデタッチに失敗しました 'MONO-PC \ SQLEXPRESS'サーバー 'MONO-PC SQLEXPRESS'のデタッチデータベースに失敗しました

Public Sub bk() 
     Try 
      Dim strDatabasePath As String = My.Computer.FileSystem.CombinePath(My.Application.Info.DirectoryPath, "LIC.mdf") 
      Dim strdbLogPath As String = My.Computer.FileSystem.CombinePath(My.Application.Info.DirectoryPath, "LIC_log.ldf") 
      ' DB.Connection can be any valid SQLConnection which you might already be using in your application 
      Dim con As New SqlClient.SqlConnection(LIC.My.Settings.LICConnectionString) 
      Dim srvCon As New ServerConnection(con) 
      Dim srv As Server = New Server(srvCon) 
      If srv.Databases.Contains(strDatabasePath) Then 
       If Not con.State = ConnectionState.Closed Then 
        con.Close() 
       End If 
       srv.DetachDatabase(strDatabasePath, True) 
       My.Computer.FileSystem.CopyFile(strDatabasePath, "c:\backup\LIC.mdf", True) 
       My.Computer.FileSystem.CopyFile(strdbLogPath, "c:\backup\LIC_log.ldf", True) 
       MessageBox.Show("Backup taken successfully") 
      End If 
      srvCon.Disconnect() 
      con.Open() 
     Catch ex As SqlException 
      MessageBox.Show(ex.Message) 
     End Try 
    End Sub 

なぜそれが失敗するのですか?ありがとうございました。

+0

少なくとも、エラー/例外が表示されますか? –

+0

"サーバーのデタッチに失敗しました 'MONO-PC \ SQLEXPRESS'これは、この行 'srv.DetachDatabase(strDatabasePath、True)'が実行されるときに得られるものです。 –

答えて

1

データベース名のパスを使用しないでください。データベースオブジェクトからファイル名を抽出する必要があります。以下は、どのデータベースでも動作する書き換えです。

Public Sub bk() 
    Try 
     Using con As New SqlClient.SqlConnection(LIC.My.Settings.LICConnectionString) 
      Dim sDatabaseName As String 

      con.Open() 

      sDatabaseName = con.Database 

      con.Close() 

      Dim srvCon As New ServerConnection(con) 
      Dim srv As Server = New Server(srvCon) 

      If srv.Databases.Contains(sDatabaseName) Then 
       Dim oDatabase As Database 
       Dim cFiles As New List(Of String) 

       ' Get a local reference to the database 
       oDatabase = srv.Databases(sDatabaseName) 

       ' Collect the list of database files associated with this database 
       For Each oFileGroup As FileGroup In oDatabase.FileGroups 
        For Each oFile As DataFile In oFileGroup.Files 
         cFiles.Add(oFile.FileName) 
        Next 
       Next 

       ' And collect the list of log files associated with this database 
       For Each oFile As LogFile In oDatabase.LogFiles 
        cFiles.Add(oFile.FileName) 
       Next 

       ' Ensure nothing is using the database 
       srv.KillAllProcesses(sDatabaseName) 

       ' Detach the database 
       srv.DetachDatabase(sDatabaseName, False) 

       ' And finally, copy all of the files identified above to the backup directory 
       For Each sFileName As String In cFiles 
        System.IO.File.Copy(sFileName, System.IO.Path.Combine("c:\backup\", System.IO.Path.GetFileName(sFileName)), True) 
       Next 

       MessageBox.Show("Backup taken successfully") 
      End If 
      srvCon.Disconnect() 
     End Using 
    Catch ex As Exception 
     MessageBox.Show("Error Occured : " & ex.Message) 
    End Try 
End Sub 
関連する問題