none
Change to MDF filename causes attach failure in vb.net RRS feed

  • Question

  • Good Morning

    I am currently using the follwoing in my install routine, the idea is to check if the db exists. if the db does exist then leave the mdf name as is and attach it as a temp db then i can run a compare between the 2 db. however if the db does not exist i want to rename the mdf and attach it. however after renaming the mdf i cant attach the file.

    any help would be most aprreciated as to why the mdf wont attach after the file name has been changed. also if you have a different way to achieve this i am open to hearing that as well

    svr = New Server(".\SQLEXPRESS")

            Dim InstallPath As String = System.IO.Path.GetDirectoryName(Me.Context.Parameters("AssemblyPath"))
            InstallPath = InstallPath.Replace("Division Manager", "Database")

            If svr.Databases.Contains("ABC") Then


            Else
                Try
                    If IO.File.Exists(InstallPath & "\RSDB.mdf") Then
                        IO.File.Delete(InstallPath & "\RSDB.mdf")
                    End If
                    'rename the db files then attach
                    Dim fiDB As New IO.FileInfo(InstallPath & "\tmpRSDB.mdf")
                    fiDB.MoveTo(InstallPath & "\RSDB.mdf")
                    'db does not exist
                    'attach the database
                    DatabaseFiles = New StringCollection
                    DatabaseFiles.Add(InstallPath & "\RSDB.mdf")
                    svr.AttachDatabase("ABC", DatabaseFiles)

                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If

    Monday, May 17, 2010 1:57 PM

Answers

  • I personally would prefer to use the Backup and restore method to attach the database files, as I have found in the pass that the attach detach methods are not the best and can cause problems. Using the backup restor method would also allow the install to use the default database and log file paths that the DBA has set up on the system.
    http://www.virtualrealm.com.au - XNA Game Programming News and Resources from Downunder.
    • Marked as answer by KJian_ Wednesday, May 26, 2010 8:22 AM
    Monday, May 17, 2010 10:59 PM
  • Hi TONCAL,

    Could you please elaborate a bit? What is the error message? It’s a Winform application, right? Which version of Windows do you have? If it’s Windows Vista or Windows 7, I think the failure may be caused by User Account Control (UAC).

    I made a simple test on my machine (Visual Studio 2008,SQL Server 2008,Windows 7):

        Dim srv As Server
        srv = New Server()
    
        Dim fiDB As New IO.FileInfo("D:\tmpRSDB.mdf")
        fiDB.MoveTo("D:\RSDB.mdf")
    
        Dim DatabaseFiles As StringCollection
        DatabaseFiles = New StringCollection
    
        DatabaseFiles.Add("D:\RSDB.mdf")
        'DatabaseFiles.Add("D:\tmpRSDB_log.ldf")
        srv.AttachDatabase("ABC", DatabaseFiles)

    It works fine if I right-click the Visual Studio and then click Run as administrator. Otherwise, I will receive the following error message:

    Attach database failed for Server 'ServerName'. Unable to open the physical file "D:\RSDB.mdf". Operating system error 5: "5(failed to retrieve text for this error. Reason: 15105)

    Related links:

    How to use User Account Control (UAC) in Windows Vista
    http://support.microsoft.com/kb/922708

    Step 6: Create and Embed an Application Manifest (UAC)
    http://msdn.microsoft.com/en-us/library/bb756929.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by KJian_ Wednesday, May 26, 2010 8:22 AM
    Wednesday, May 19, 2010 7:54 AM

All replies

  • I personally would prefer to use the Backup and restore method to attach the database files, as I have found in the pass that the attach detach methods are not the best and can cause problems. Using the backup restor method would also allow the install to use the default database and log file paths that the DBA has set up on the system.
    http://www.virtualrealm.com.au - XNA Game Programming News and Resources from Downunder.
    • Marked as answer by KJian_ Wednesday, May 26, 2010 8:22 AM
    Monday, May 17, 2010 10:59 PM
  • Hi TONCAL,

    Could you please elaborate a bit? What is the error message? It’s a Winform application, right? Which version of Windows do you have? If it’s Windows Vista or Windows 7, I think the failure may be caused by User Account Control (UAC).

    I made a simple test on my machine (Visual Studio 2008,SQL Server 2008,Windows 7):

        Dim srv As Server
        srv = New Server()
    
        Dim fiDB As New IO.FileInfo("D:\tmpRSDB.mdf")
        fiDB.MoveTo("D:\RSDB.mdf")
    
        Dim DatabaseFiles As StringCollection
        DatabaseFiles = New StringCollection
    
        DatabaseFiles.Add("D:\RSDB.mdf")
        'DatabaseFiles.Add("D:\tmpRSDB_log.ldf")
        srv.AttachDatabase("ABC", DatabaseFiles)

    It works fine if I right-click the Visual Studio and then click Run as administrator. Otherwise, I will receive the following error message:

    Attach database failed for Server 'ServerName'. Unable to open the physical file "D:\RSDB.mdf". Operating system error 5: "5(failed to retrieve text for this error. Reason: 15105)

    Related links:

    How to use User Account Control (UAC) in Windows Vista
    http://support.microsoft.com/kb/922708

    Step 6: Create and Embed an Application Manifest (UAC)
    http://msdn.microsoft.com/en-us/library/bb756929.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by KJian_ Wednesday, May 26, 2010 8:22 AM
    Wednesday, May 19, 2010 7:54 AM