Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  Data Programming  »  SQL Server  »  An introduction to SQL Server Management Objects  »  Backup and restore databases
An introduction to SQL Server Management Objects
by David Jeavons | Published  01/02/2007 | SQL Server | Rating:
David Jeavons
I have been programming database applications for almost 12 years starting out with VB3 and using every major version since then. I currently work for a retailer analysis company writing ASP.NET code for our ever evolving web site. I also enjoy helping other programmers with Visual Basic and .NET and am a moderator at vbCity. 

View all articles by David Jeavons...
Backup and restore databases

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.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.41747572815534 out of 5
 103 people have rated this page
Article Score33973
Sponsored Links