title: SQL Database backup using SMO and VB.NET
body: <p>I am trying to run this seemingly simple piece of code which is repeated several times all over the net, but I am getting errors in the code:</p>
  
  <pre><code>Imports System.Data.SqlClient
  Imports Microsoft.SqlServer.Management.Smo
  Imports Microsoft.SqlServer.Management.Common
  
  Private Sub BackupDatabaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackupDatabaseToolStripMenuItem.Click
  'Connect to the local, default instance of SQL Server.
  Dim srv As Server
  srv = New Server
  'Reference the AdventureWorks2008R2 database.
  Dim db As Database
  db = srv.Databases("AdventureWorks2008R2")
  'Store the current recovery model in a variable.
  Dim recoverymod As Integer
  recoverymod = db.DatabaseOptions.RecoveryModel
  'Define a Backup object variable. 
  Dim bk As New Backup
  'Specify the type of backup, the description, the name, and the database to be backed up.
  bk.Action = BackupActionType.Database
  bk.BackupSetDescription = "Full backup of AdventureWorks2008R2"
  bk.BackupSetName = "AdventureWorks2008R2 Backup"
  bk.Database = "AdventureWorks2008R2"
  'Declare a BackupDeviceItem by supplying the backup device file name in the constructor, and the type of device is a file.
  Dim bdi As BackupDeviceItem
  bdi = New BackupDeviceItem("Test_Full_Backup1", DeviceType.File)
  'Add the device to the Backup object.
  bk.Devices.Add(bdi)
  'Set the Incremental property to False to specify that this is a full database backup.
  bk.Incremental = False
  'Set the expiration date.
  Dim backupdate As New Date
  backupdate = New Date(2006, 10, 5)
  bk.ExpirationDate = backupdate
  'Specify that the log must be truncated after the backup is complete.
  bk.LogTruncation = BackupTruncateLogType.Truncate
  'Run SqlBackup to perform the full database backup on the instance of SQL Server.
  bk.SqlBackup(srv)
  End Sub
  </code></pre>
  
  <p>I am getting errors on these lines:
  Dim bk As New Backup</p>
  
  <p><em>Error  5   Type 'Backup' is not defined.</em></p>
  
  <p>bk.Action = BackupActionType.Database</p>
  
  <p><em>Error  6   'BackupActionType' is not declared. It may be inaccessible due to its protection level.</em></p>
  
  <p>Dim bdi As BackupDeviceItem</p>
  
  <p><em>Error  7   Type 'BackupDeviceItem' is not defined.</em></p>
  
  <p>along with other similar errors</p>
  
  <p>Am I missing some "imports"?
  I got the code from here: <a href="http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.backup.sqlbackup.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.backup.sqlbackup.aspx</a></p>