Private Sub RibbonGroup_SupportSpaceTools_DatabaseManagement_RestoreDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RibbonGroup_SupportSpaceTools_DatabaseManagement_RestoreDatabase.Click
If MessageBox.Show("You are about to restore the " & ProductName & " database. Please be advised that if you click on the " & _
"Yes button, the database restore will start and the restoration will completely overwrite the current database. " & _
"Any updates or changes you have made to the current database on any date after the last backup will be lost. " & _
"Once the overwrite has been completed, the changes made by the database restore cannot be reversed. Please " & _
"click Yes to continue or click No to cancel the restore.", _
"Database Restoration Confirmation", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning, _
MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
'Yes was clicked on by the end user, restore the database.
With diagRestoreDB
.AddExtension = True
.CheckFileExists = True
.CheckPathExists = True
.DefaultExt = "bak"
.DereferenceLinks = True
.FileName = "DatabaseBackup"
.Filter = "BAK Files|*.bak"
.FilterIndex = 1
.InitialDirectory = "C:\"
.Multiselect = False
.ReadOnlyChecked = False
.RestoreDirectory = True
.ShowReadOnly = False
.Title = ProductName & ": Database Restore"
.ValidateNames = False
If .ShowDialog = Windows.Forms.DialogResult.OK Then
Try
Using SetDatabaseConnection As New SqlConnection(GetDatabaseConnectionString)
'Specify the SQL query to backup the database.
Dim sSQL As String = "USE master;" & vbNewLine & _
"RESTORE DATABASE SupportSpace" & vbNewLine & _
"FROM DISK = " & "'" & .FileName.ToString & "'" & "WITH REPLACE"
Dim sSQLCommand As New SqlCommand
SetDatabaseConnection.Open()
With sSQLCommand
.Connection = SetDatabaseConnection
.CommandText = sSQL
.ExecuteNonQuery()
End With
SetDatabaseConnection.Close()
End Using
Catch ex As InvalidOperationException
MessageBox.Show(ex.Message, _
"Invalid Operation Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation, _
MessageBoxDefaultButton.Button1)
Catch ex As SqlException
MessageBox.Show(ex.Message, _
"SQL Exception", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation, _
MessageBoxDefaultButton.Button1)
End Try
End If
End With
Else 'No
MessageBox.Show("The database restoration successfully canceled. No database modifications were completed.", _
"Database Restore Successfully Canceled", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information, _
MessageBoxDefaultButton.Button1)
End If
End Sub