【问题标题】:Why does code not enter IF block?为什么代码不进入 IF 块?
【发布时间】: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块

标签: vb.net sql-server-2008


【解决方案1】:

我认为您的代码的主要问题是您混淆了概念。

数据库的名称与 SQL Server 用于存储数据库内容的文件的路径完全不同。

当您对Databases collectionServer 执行操作时,ContainsDetachDatabase 等操作需要数据库的名称,而不是文件的路径。

您可以从 SqlClient 连接中获取数据库的名称(在Database 属性中),您可以使用MasterDBPathMasterDBLogPath 属性从Server 对象中获取数据库文件的名称。

这使您的代码更加简洁,并且不依赖于存储在特定位置的文件。

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

【讨论】:

  • 我刚刚尝试了你的代码......还没有运气......使用你的代码,在调试模式下它甚至没有进入 IF 块。
  • 我正在使用的代码 sn-p ..在调试模式下工作正常,但部署后失败...http://www.sendspace.com/file/l08qjj我已经上传到这里...请看看...代码在 InsertData.vb 中,就在 InsertData_FormClosed 子的下方。
  • 这就是我现在想要做的......但我也失败了......http://stackoverflow.com/questions/8873226/terminate-application-using-full-path
  • 我尝试使用MsgBox(con.Database.ToString) 打印数据库名称....似乎找不到数据库名称...MsgBox 为空。
  • 啊,这是一个很大的线索!听起来您的连接字符串可能丢失或有故障。您可以尝试使用硬编码的连接字符串来验证它是否有效,然后使用连接字符串解决问题(我怀疑这是 MySettings 的问题)。我刚刚使用硬编码的连接字符串测试了代码,并验证它是否按预期工作。
【解决方案2】:

您是否检查过数据库是否在此路径上?

My.Application.Info.DirectoryPath

如您所想,您的代码只会在 srv.Databases 具有您正在寻找的路径的情况下输入该 IF。

尝试打印 srv.Databases 列表以检查其内容。

【讨论】:

  • 请解释一下..我听不懂你在说什么..抱歉打扰了。
  • 你一点也不打扰!在 IF 中设置断点。这就是我的意思blogs.interfacett.com/wp-content/uploads/2011/09/…
  • C:\Users\mono\AppData\Local\Apps\2.0\Data\J17BC107.O4P\Y8KA1TGQ.51R\lic...tion_4d5f0e541bcb5349_0001.0000_a2ca4a03febcb75f\Data\Lic.mdf这是我从strDatabasePath得到的
  • 我敢打赌,该路径在 srv.Databases 列表中不存在。这就是您的代码不输入 If 语句的原因。也许你需要某种字符串处理来做你想要的检查。
  • Application.UserAppDataPath 获取.mdf 文件的唯一方法吗?没有其他方法吗??我的意思是在制作.exe 之后...调试My.Application.Info.DirectoryPath 工作正常.
猜你喜欢
  • 1970-01-01
  • 2015-05-07
  • 2012-05-16
  • 2013-10-30
  • 2018-07-13
  • 2012-04-19
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
相关资源
最近更新 更多