Introduction
The need to back up databases on a regular basis is a major component of managing any production system. This post presents a simple way in which you could incorporate a backup tool from with in your application.
Simply download the source code with the backup form that you need to just add into your application
Key Methods include
LoadServers; this method loads all local servers
''' <summary>
''' Loads all local servers
''' </summary>
''' <remarks></remarks>
Private Sub LoadServers()
Try
Me.Cursor = Cursors.WaitCursor
Dim servers As New DataTable()
servers = SmoApplication.EnumAvailableSqlServers(True)
Me.cboServerName.Items.Clear()
For Each server As DataRow In servers.Rows
Me.cboServerName.Items.Add(server.Item("Name"))
Next
Catch ex As Exception
Return
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
LoadDatabases; this method loads databases for the selected server
''' <summary>
''' Loads databases for the selected server
''' </summary>
''' <param name="serverName"></param>
''' <remarks></remarks>
Private Sub LoadDatabases(ByVal serverName As String)
Try
Me.Cursor = Cursors.WaitCursor
Me.cboDataBaseName.Items.Clear()
Dim server As New Server(serverName)
For Each database As Database In server.Databases
Me.cboDataBaseName.Items.Add(database.Name)
Next
Catch ex As Exception
Return
Finally
Me.Cursor = Cursors.Default
End Try
End Sub
Backup button performs the backup process
Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
Try
Cursor.Current = Cursors.WaitCursor
Dim serverName As String = Me.cboServerName.Text.ToString()
Dim fileName As String = Me.txtBackupFile.Text.ToString()
If String.IsNullOrEmpty(serverName) Then Throw New ArgumentException("Enter server name")
If String.IsNullOrEmpty(fileName) Then Throw New ArgumentException("Load backup file!")
Dim backupServer As New Server(serverName)
Dim dataBackup As New Backup()
dataBackup.Action = BackupActionType.Database
dataBackup.Database = Me.cboDataBaseName.Text.ToString()
''set the devices: file, tape etc
dataBackup.Devices.AddDevice(fileName, DeviceType.File)
''set this if you want to do incremental
dataBackup.Incremental = Me.chkIncremental.Checked
''progress meter stuff
Me.proBackupDatabase.Minimum = 0
Me.proBackupDatabase.Value = 10
Me.proBackupDatabase.Maximum = 100
''this gives the % complete by handling the event
''provided by SMO on the percent complete; we will
''update the progress meter in the event handler
''set the progress meter to 10% by default
dataBackup.PercentCompleteNotification = 10
AddHandler dataBackup.PercentComplete, AddressOf Me.BackupProgressEventHandler
dataBackup.SqlBackup(backupServer)
MessageBox.Show("Backup completed successfully!")
Catch exSMO As SmoException
Me.proBackupDatabase.Value = 0
MessageBox.Show(exSMO.Message)
Catch ex As Exception
Me.proBackupDatabase.Value = 0
MessageBox.Show(ex.Message)
Finally
Me.proBackupDatabase.Value = 0
Cursor.Current = Cursors.Default
End Try
End Sub
Private Sub BackupProgressEventHandler(ByVal sender As System.Object, ByVal e As PercentCompleteEventArgs)
''increase the progress bar up by the percent
Me.proBackupDatabase.Value = e.Percent
End Sub
Related Post:
Restore MS SQL Server database from within your application(s)