Wilson Kutegeka

Microsoft MVP - Visual Basic www.clinicmaster.net

Backup MS SQL Server database from within your application(s)


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

Backup MS SQL Server database from within 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)

 

Comments

Wilson Kutegeka said:

Introduction This post is a follow-up of my previous post; Backup MS SQL Server database from within

# September 4, 2009 10:48 AM

miagale said:

Hi I am searching blog backup utility.Can u tell me about WP-DB-Backup, what is this?

# July 20, 2010 12:34 PM

Divyang said:

Pls post the article that can describe how to do that increase the progress bar up by the percent when we use it in sql procedure.

# September 23, 2010 1:35 PM

javier said:

hola, muy bueno pero necesito lo mismo pero para aplicaciones web

# October 30, 2010 3:09 AM

Technical Support Outsourcing said:

Great Blog, I've learned a lot already! I really love this QueryString class you've written. Would you mind if I used some of it in a similar how to post for flash but rewriting it for as2?

# March 19, 2011 9:41 AM

Wilson Kutegeka said:

No problem, go ahead to use it

# March 21, 2011 1:55 PM

Patrick Miron said:

I used your application to create mine. I have come up with a little problem. I want to backup the file to my local machine and its trying to backup onto the server and since of course I don't have the same directory structure on the server it fails. The opposite happens when I backup local database, I can't back them up on the server.

My question is that, is there a property to BackupDeviceList or type that we can set to choose the "local" drive instead of the "current" server location drive?

# April 5, 2011 12:39 AM

Peter Bamuhigire said:

Thanks for the application. Just a note, You have to run it as 'Administrator' if You are using windows 7, do the same to Your application if You embed this code.

# February 27, 2012 6:29 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 3 and 1 and type the answer here: