Vb.net 分离服务器';的数据库失败;MONO-PC\SQLEXPRESS';

Vb.net 分离服务器';的数据库失败;MONO-PC\SQLEXPRESS';,vb.net,sql-server-2008,Vb.net,Sql Server 2008,错误:“为服务器'MONO-PC\SQLEXPRESS'分离数据库失败。” 为什么失败?感谢您的帮助。您不应该使用数据库名称的路径,而应该从数据库对象中提取文件名。以下是适用于任何数据库的重写: Public Sub bk() Try Using con As New SqlClient.SqlConnection(LIC.My.Settings.LICConnectionString) Dim sDatabaseName As String

错误:“为服务器'MONO-PC\SQLEXPRESS'分离数据库失败。”


为什么失败?感谢您的帮助。

您不应该使用数据库名称的路径,而应该从数据库对象中提取文件名。以下是适用于任何数据库的重写:

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

您至少可以发布您收到的错误/异常吗?“为服务器'MONO-PC\SQLEXPRESS'分离数据库失败”。这是我在它执行此行时得到的结果。
srv.DetachDatabase(strDatabasePath,True)
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