【发布时间】:2012-01-15 16:35:31
【问题描述】:
我正在使用给定的代码来复制我的数据库文件......它在调试模式下就像一个魅力,但一旦我创建一个设置,它就会停止工作。错误是
“数据库分离失败”
我尝试逐行检查代码,发现代码没有进入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
【问题讨论】:
-
欢迎来到stackoverflow @user1150440
-
谢谢...你能解释一下“为什么投反对票以及我的问题的任何解决方案”吗? :P
-
它为您的普通 n00b 提供了一次动力之旅,并在他们原本平静的生活中获得了成就感
-
尝试在 if 语句之前打印“strDatabasePath”的值,并确保它包含您所期望的内容
-
我试过了......它指向.mdf文件..但没有进入IF块