2012-01-15 23 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

你能至少發佈錯誤/異常,你得到? – 2012-01-15 06:59:03

+0

「服務器'MONO-PC \ SQLEXPRESS'的分離數據庫失敗。」這是我執行這行時得到的'srv.DetachDatabase(strDatabasePath,True)' – 2012-01-15 07:02:13

回答

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