Results 1 to 6 of 6

Thread: Backup & Restore: What exactly wrong with my vb.net code?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    14

    Backup & Restore: What exactly wrong with my vb.net code?

    The following code backs up sql data successfully but Restoring the database is not successful. Please what's exactly wrong with the restore code. One thing i noticed is that the code runs perfectly without any error. Just that restore doesnt take effect after it says restore complete. Also, how do I add a backup and restore progress bar to it for user to see the progress of their activities. Thank you.

    Code:
       'backup
    
            Dim stempel As String = (Date.Today.Day.ToString & "_" &
                                     Date.Today.Month.ToString & "_" & Date.Today.Year.ToString & "_" &
                                     Date.Now.Hour.ToString & "_" & Date.Now.Minute.ToString & "_" &
                                     Date.Now.Second.ToString & " hrs")
    
            Dim backup_name As String = stempel
            Dim backupcommand As String = "backup database [" & database & "] to Disk='" & My.Computer.FileSystem.SpecialDirectories.MyDocuments.ToString & "\NumbersBackup" & stempel & ".bak'"
    
            Dim cmd As SqlCommand = New SqlCommand(backupcommand, con)
    
            Try
                MsgBox("The Backup page will close when the Backup is done. Click OK and Wait!", vbInformation, "Backup Database")
                con.Open()
                cmd.ExecuteNonQuery()
            Catch ex As Exception
    
            End Try

    Code:
     'Restore
    
            BackupOpenFileDialog.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).ToString
            BackupOpenFileDialog.Filter = "Backup Files|*.bak"
            BackupOpenFileDialog.ShowDialog()
            lblBackupFileLocation.Text = BackupOpenFileDialog.FileName.ToString
    
            If lblBackupFileLocation.Text = "Select Backup File To Restore" Then
            Else
    
                ''Drops database
                Dim dropcommand As String = "Use [Master]; ALTER DATABASE [" & database & "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [" & database & "]"
                Dim dropcmd As SqlCommand = New SqlCommand(dropcommand, con)
    
                'Retores database
    
                Dim restorecommand As String = "Use [Master]; Restore Database [" & database & "] from Disk='" & BackupOpenFileDialog.FileName & "' with File = 1, Move 'cms' to '" & database _
                                                       & "', Move 'cms_log' to '" & databaselog & "', Replace"
    
    
                Dim cmd As SqlCommand = New SqlCommand(restorecommand, con)
    
                Try
                    MsgBox("The Restore page will close when the Restore is done. Click OK and Wait!", vbInformation, "Restore Backup")
                    con.Open()
                    cmd.ExecuteNonQuery()
                Catch ex As Exception
    
                Finally
                    MsgBox("Restore Completed!", vbInformation, "Backup/Restore")
                    con.Close()
                    con.Dispose()
                End Try

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Backup & Restore: What exactly wrong with my vb.net code?

    Well of course it reports that it is done... 1) Your catch is empty ... what you've done is the .NET equivalent of an On Error Resume Next ... there could be an error happening but you've chosen to stuff your fingers in your ears and go "Blah blah blah" and ignore it. 2) The finally block will ALWAYS run even when there is an error, so to have the message box in there that said it completed is a red-herring.... well... maybe... but it insinuates that it completed successfully, which it may or may not have done, but you'll never know. See point #1. Move the message box to after your ExecuteNonQuery ... and add SOMETHING to your catch block so you can see if something goes wrong... and ... well... find out if something is going wrong... I bet something is.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    14

    Re: Backup & Restore: What exactly wrong with my vb.net code?

    Quote Originally Posted by techgnome View Post
    Well of course it reports that it is done... 1) Your catch is empty ... what you've done is the .NET equivalent of an On Error Resume Next ... there could be an error happening but you've chosen to stuff your fingers in your ears and go "Blah blah blah" and ignore it. 2) The finally block will ALWAYS run even when there is an error, so to have the message box in there that said it completed is a red-herring.... well... maybe... but it insinuates that it completed successfully, which it may or may not have done, but you'll never know. See point #1. Move the message box to after your ExecuteNonQuery ... and add SOMETHING to your catch block so you can see if something goes wrong... and ... well... find out if something is going wrong... I bet something is.

    -tg
    Thanks Techgnome, after following your instruction i had the error:
    "Exclusive access could not be obtained because the database is in use".

    What do you suggest i do to solve this?
    Thanks.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Backup & Restore: What exactly wrong with my vb.net code?

    Make sure it's not in use... IE, there are no open connections to it... by anyone. You can't restore a database if it has any open connections to it. I'd suggest that if you plan to do restore operations, that when you connect to the server, you just connect to master to begin with... don't connect to the database, then do a "use master" ... if you're the only user in the database, I'd suspect that's what the issue is. doing a backup is far easier, it doesn't care about connections, but with a restore it does because it has to disconnect the db from the server in order to restore the files before reconnecting them and marking the db ready for use.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    14

    Re: Backup & Restore: What exactly wrong with my vb.net code?

    Quote Originally Posted by techgnome View Post
    Make sure it's not in use... IE, there are no open connections to it... by anyone. You can't restore a database if it has any open connections to it. I'd suggest that if you plan to do restore operations, that when you connect to the server, you just connect to master to begin with... don't connect to the database, then do a "use master" ... if you're the only user in the database, I'd suspect that's what the issue is. doing a backup is far easier, it doesn't care about connections, but with a restore it does because it has to disconnect the db from the server in order to restore the files before reconnecting them and marking the db ready for use.

    -tg
    i have figured it out.

    I initially didn't execute the drop command that was why;
    Code:
     Dim dropcommand As String = "Use [Master]; ALTER DATABASE [" & database & "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [" & database & "]"
                Dim dropcmd As SqlCommand = New SqlCommand(dropcommand, con)
                con.Open()
                dropcmd.ExecuteNonQuery()

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    14

    Re: Backup & Restore: What exactly wrong with my vb.net code?

    Quote Originally Posted by techgnome View Post
    Make sure it's not in use... IE, there are no open connections to it... by anyone. You can't restore a database if it has any open connections to it. I'd suggest that if you plan to do restore operations, that when you connect to the server, you just connect to master to begin with... don't connect to the database, then do a "use master" ... if you're the only user in the database, I'd suspect that's what the issue is. doing a backup is far easier, it doesn't care about connections, but with a restore it does because it has to disconnect the db from the server in order to restore the files before reconnecting them and marking the db ready for use.

    -tg
    i have figured it out. Thanks for your help.
    I initially didn't execute the drop command that was why;
    Code:
     Dim dropcommand As String = "Use [Master]; ALTER DATABASE [" & database & "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [" & database & "]"
                Dim dropcmd As SqlCommand = New SqlCommand(dropcommand, con)
                con.Open()
                dropcmd.ExecuteNonQuery()

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width