首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用发布向导创建安装程序后,代码将停止工作。

使用发布向导创建安装程序后,代码将停止工作。
EN

Stack Overflow用户
提问于 2012-01-15 14:17:41
回答 1查看 149关注 0票数 0
代码语言:javascript
复制
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

我正在使用给定的代码复制我的数据库,files...it在调试模式下工作起来就像一个符咒,但是一旦我创建了一个安装程序

...it停止working...the错误是“数据库分离失败”.我试着逐行检查代码,发现代码

不输入IF块。.i不知道why...can我在这方面得到了一些帮助??

EN

回答 1

Stack Overflow用户

发布于 2012-01-15 20:24:20

问题是您使用的是数据库路径,而不是数据库名称。您可以使用此代码解决问题:

代码语言:javascript
复制
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 srvCon As New ServerConnection(con)
            Dim srv As Server = New Server(srvCon)

            If srv.Databases.Contains(con.Database) Then
                srv.KillAllProcesses(con.Database)

                srv.DetachDatabase(con.Database, True)

                System.IO.File.Copy(srv.MasterDBPath, "c:\backup\LIC.mdf", True)
                System.IO.File.Copy(srv.MasterDBLogPath, "c:\backup\LIC_log.ldf", True)

                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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8870321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档