JayBeeOH

Backup Database Demo

Jun 11th, 2015
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.99 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2015. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Backup Database Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   June 2015
  16. '
  17. ' Description:    Simple Database Backup Demo, backing up the Payables Database.
  18. '
  19. '                 Documentation is at:
  20. '                   App's Visual Basic .NET code is at http://pastebin.com/u/jaybeeoh
  21. '                   Video tutorials at YouTube: http://www.youtube.com/user/bolenpresents
  22. '-------------------------------------------------------------------------------------------
  23.  
  24. Imports System.Data.SqlClient
  25.  
  26. Public Class BackupDBForm
  27.  
  28.     Private Sub BackupDBButton_Click(sender As Object, e As EventArgs) Handles BackupDBButton.Click
  29.  
  30.         Dim TargetPathFile As String = "U:\My Backups\Payables_" & DateTime.Today.ToString("yyyy-MM-dd") & ".bak"
  31.         Dim ConnStr As String = "Data Source=.\SQLEXPRESS;Initial Catalog=Payables;Integrated Security=True"
  32.         Dim sqlQuery As String = "BACKUP DATABASE Payables TO DISK = @TargetPathFile;"
  33.  
  34.         Using con As New SqlConnection(ConnStr)
  35.             Dim cmd As New SqlCommand(sqlQuery, con)
  36.             cmd.Parameters.AddWithValue("@TargetPathFile", TargetPathFile)
  37.  
  38.             Try
  39.                 con.Open()
  40.                 Dim sqlReturnValue As Integer = cmd.ExecuteNonQuery
  41.                 con.Close()
  42.                 If sqlReturnValue <> -1 Then
  43.                     MessageBox.Show("SQL Response value was: " & sqlReturnValue.ToString(),
  44.                                     "SQL Response",
  45.                                     MessageBoxButtons.OK,
  46.                                     MessageBoxIcon.Information)
  47.                 Else
  48.                     MessageBox.Show("DataBase Payables has been backed up to to disk successfully.",
  49.                                     "DB Backup Completed",
  50.                                     MessageBoxButtons.OK,
  51.                                     MessageBoxIcon.Information)
  52.                 End If
  53.             Catch ex As SqlException
  54.                 MessageBox.Show("SQL Server error # " & ex.Number & ": " & ex.Message,
  55.                             ex.GetType.ToString,
  56.                             MessageBoxButtons.OK,
  57.                             MessageBoxIcon.Error)
  58.             End Try
  59.         End Using
  60.     End Sub
  61. End Class
Advertisement
Add Comment
Please, Sign In to add comment