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




Reply With Quote
