我正在使用给定的代码复制我的数据库,files...it在调试模式下工作起来就像一个魅力,但是一旦我创建了一个设置,它就停止工作了。错误是
“数据库分离失败”
我试着逐行检查代码,发现代码没有进入IF块。
我不知道为什么。
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")
''# Dim strDatabasePath As String = My.Computer.FileSystem.CombinePath(Application.UserAppDataPath, "LIC.mdf")
''# Dim strdbLogPath As String = My.Computer.FileSystem.CombinePath(Application.UserAppDataPath, "LIC_log.ldf")
MsgBox(Application.UserAppDataPath)
''# 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)
MsgBox(srv.ToString)
If srv.Databases.Contains(strDatabasePath) Then
MsgBox("In If")
If con.State = ConnectionState.Open Then
MsgBox(con.State)
con.Close()
End If
MsgBox(con.State & " Is It True?")
srv.KillAllProcesses(My.Computer.FileSystem.CombinePath(My.Application.Info.DirectoryPath, "LIC.mdf"))
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 Exception
MessageBox.Show("Error Occured : " & ex.Message)
End Try
End Sub发布于 2012-01-15 20:20:24
我认为您的代码的主要问题是您正在混合概念。
数据库名称与Server用于存储数据库内容的文件路径完全不同。
当您在数据库集合和服务器上执行操作时,Contains和DetachDatabase等操作需要数据库的名称,而不是文件的路径。
您可以从SqlClient连接(在Database属性中)获取数据库的名称,也可以使用MasterDBPath和MasterDBLogPath属性从MasterDBPath对象获取数据库文件的名称。
这使得您的代码更加简洁,并且不依赖于存储在特定位置的文件。
Public Sub bk()
Try
''# DB.Connection can be any valid SQLConnection which you might already be using in your application
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发布于 2012-01-15 16:44:55
您检查过数据库是否在此路径上吗?
My.Application.Info.DirectoryPath正如您想象的那样,只有在srv.Databases有您要寻找的路径的情况下,您的代码才会输入该路径。
尝试打印srv.Databases列表以检查它的内容。
https://stackoverflow.com/questions/8871330
复制相似问题