Basics what you need... The assemblies can be found in c:\program files\Microsoft SQL Server\XXX\SDK\Assemblies\... (XXX = Version of your SQL Server installed.) ONLY BACKS UP DATABASE< NOT A SINGLE TABLE
Imports System.IO
Imports Microsoft.SqlServer.Management
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Server
Imports Microsoft.SqlServer.Management.Common
Imports Microsoft.SqlServer.Management.Smo.Agent
Imports Microsoft.SqlServer.Management.Smo.Broker
Imports Microsoft.SqlServer.Management.Smo.Mail
Imports Microsoft.SqlServer.Management.Smo.RegisteredServers
Imports Microsoft.SqlServer.Management.Smo.Wmi
Private Class BackupDatabase
Public Sub CreateBackup()
Dim bckupDatabase As New Backup
bckupDatabase.Action = BackupActionType.Database
Dim myServer As Server = New Server("YOURSERVERNAMEHERE")
Dim myDatabase As Database = myServer.Databases("YOURDATABASENAME")
bckupDatabase.Database = myDatabase.Name
Dim int As Integer = txtLocation.Text.Length
If int = 3 Then
bckupDatabase.Devices.AddDevice(txtLocation.Text + "\BACKUPDATABASENAME.dll", DeviceType.File)
ElseIf int > 3 Then
bckupDatabase.Devices.AddDevice(txtLocation.Text + "BACKUPDATABASENAME.dll", DeviceType.File)
End If
bckupDatabase.BackupSetName = "TYPE DATABASE BACKUP NAME HERE"
bckupDatabase.BackupSetDescription = "TYPE DATABASE BACKUP DESCRIPTION (eg. Full Bacup) HERE"
bckupDatabase.Initialize = False
AddHandler bckupDatabase.PercentComplete, AddressOf CompletionStatusInPercent
AddHandler bckupDatabase.Complete, AddressOf Backup_Completed
bckupDatabase.SqlBackup(myServer)
Catch ex As Exception
MessageBox.Show(ex.Message & ex.StackTrace)
End Try
End Sub
Private Sub CompletionStatusInPercent(sender As Object, args As PercentCompleteEventArgs)
txtStatusLog.Text = String.Format("Percent completed: {0}%.", args.Percent)
End Sub
Private Sub Backup_Completed(sender As Object, args As ServerMessageEventArgs)
txtStatusLog.Text = txtStatusLog.Text & vbNewLine & ("Success...Backup completed.")
txtStatusLog.Text = txtStatusLog.Text & vbNewLine & (args.Error.Message)
End Sub
End Class