Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7062599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:36:47+00:00 2026-05-28T04:36:47+00:00

I am using the given code to copy my database files…it works like a

  • 0

I am using the given code to copy my database files…it works like a charm in debug mode but as soon as I create a setup, it stops working. The error is

“Database Detach Failed”

I tried checking the code line by line and found that the code does not enter the IF block.

I have no idea why.

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:36:48+00:00Added an answer on May 28, 2026 at 4:36 am

    I think the primary problem with your code is that you are mixing concepts.

    The name of the database is completely different than the path to the file(s) that SQL Server uses to store the contents of the database.

    When you are performing operations on the Databases collection and the Server, the operations such as Contains and DetachDatabase expect the name of the database, not the path to the files.

    You can obtain the name of the Database from the SqlClient connection (in the Database property) and you can obtain the name of the database files from the Server object using the MasterDBPath and MasterDBLogPath properties.

    This makes your code much cleaner and not dependent on the files being stored in specific locations.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using the following code to create various struct, but only give people
Given the following code: using (var client = new WebClient()) { string url =
Given the following lines of code which is using JQTOUCH: $('#customers').bind('pageAnimationEnd', function(e, info){ if
I'm using this code, to make a request to a given URL: private static
I'm using SQLBULKCOPY to copy some data-tables into a database table, however, because the
I'm using SqlClient.SqlBulkCopy to try and bulk copy a csv file into a database.
I'm relatively new to PowerShell, and I was trying to copy files using a
I gave someone a copy of my code using hg archive a while ago.
Python 2.5.1 http://www.cgsecurity.org/wiki/After_Using_PhotoRec I've just run PhotoRec and the code given as a way
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.