Hi,

I thought when you use "using", it opens and closes the database connection when needed automatically. I am not sure if I am doing this right but would like to know if you can advise if it can be improved. I am able to create the BAK file without issue and I do not get any errors. Thanks.

vb Code:
  1. Private Sub RibbonGroup_SupportSpaceTools_DatabaseManagement_BackupDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RibbonGroup_SupportSpaceTools_DatabaseManagement_BackupDatabase.Click
  2.         With diagBackupDB
  3.             .AddExtension = True
  4.             .CheckFileExists = True
  5.             .CheckPathExists = True
  6.             .CreatePrompt = True
  7.             .DefaultExt = "bak"
  8.             .DereferenceLinks = True
  9.             .FileName = "DatabaseBackup"
  10.             .Filter = "BAK Files|*.bak"
  11.             .FilterIndex = 1
  12.             .InitialDirectory = "C:\"
  13.             .OverwritePrompt = True
  14.             .RestoreDirectory = True
  15.             .Title = ProductName & ": Database Backup"
  16.             .ValidateNames = True
  17.             .ShowDialog()
  18.  
  19.             If CBool(Windows.Forms.DialogResult.OK) Then
  20.                 Try
  21.                     Using SetDatabaseConnection As New SqlConnection(GetDatabaseConnectionString)
  22.                         'Specify the SQL query to backup the database.
  23.                         Dim sSQL As String = "BACKUP DATABASE SupportSpace" & vbNewLine & _
  24.                                              "TO DISK = " & "'" & .FileName.ToString & "'" & vbNewLine & _
  25.                                              "WITH FORMAT"
  26.  
  27.                         Dim sSQLCommand As New SqlCommand
  28.  
  29.                         SetDatabaseConnection.Open()
  30.  
  31.                         With sSQLCommand
  32.                             .Connection = SetDatabaseConnection
  33.                             .CommandText = sSQL
  34.                             .ExecuteNonQuery()
  35.                         End With
  36.  
  37.                         SetDatabaseConnection.Close()
  38.                     End Using
  39.  
  40.                     'Everything went ok, advise the end user.
  41.                     MessageBox.Show("The database: SupportSpace was successfully backed up to: " & vbNewLine & vbNewLine & _
  42.                                     .FileName.ToString, _
  43.                                     "Database Backed Up", _
  44.                                     MessageBoxButtons.OK, _
  45.                                     MessageBoxIcon.Information, _
  46.                                     MessageBoxDefaultButton.Button1)
  47.                 Catch ex As InvalidOperationException
  48.                     MessageBox.Show(ex.Message, _
  49.                                     "Invalid Operation Exception", _
  50.                                     MessageBoxButtons.OK, _
  51.                                     MessageBoxIcon.Exclamation, _
  52.                                     MessageBoxDefaultButton.Button1)
  53.                 Catch ex As SqlException
  54.                     MessageBox.Show(ex.Message, _
  55.                                     "SQL Exception", _
  56.                                     MessageBoxButtons.OK, _
  57.                                     MessageBoxIcon.Exclamation, _
  58.                                     MessageBoxDefaultButton.Button1)
  59.                 End Try
  60.             End If
  61.         End With
  62.     End Sub