Backing up a database involves choosing what to backup and to which device you want to store the data. For the sake of simplicity, this section shows you how to backup a database to a file on disk, however there are tools available within the SMO that allow you to query what backup devices are available and also to choose what objects you would like to backup.
To backup an entire database to a disk file involves creating a backup object and specifying what device and database you are going to backup. The following example will backup our new database to a temp folder. Note however, that if you do not specify a location then the backup file will reside in the default Backup directory of SQL Server:
1 Dim backup As New Backup
2 backup.Action = BackupActionType.Database
3 backup.Database = "MyNewDatabase"
4 backup.Devices.AddDevice("C:\Temp\MyNewDatabaseBackup.bak", DeviceType.File)
5 backup.SqlBackup(sqlServer)
Restoring a database is very similar to backing up a database:
1 Dim restore As New Restore
2 restore.Action = RestoreActionType.Database
3 restore.Database = "MyNewDatabase"
4 restore.Devices.AddDevice("C:\Temp\MyNewDatabaseBackup.bak", DeviceType.File)
5 restore.SqlRestore(sqlServer)
Conclusion
As you can see from the few examples provided in this article, the SQL Server Management Objects libraries provide you with a number of tools for performing many powerful operations against an SQL Server. I highly recommend that you play around with these samples and look at the many different options that are available to you throughout these libraries, I am sure you will find that there are many things that you can do with surprisingly little code.