Simple Backup/Restore Utility With SQL-DMO Using VB.NET - Understanding Backup and Restore processes
(Page 5 of 5 )
The following is the code to work with the Backup process using SQL-DMO:
Private Sub doBackup()
With oBackup
'.Devices = "[NorthwindBackup]"
.Files = _BACKUPFILE
.Database = _DATABASE
.BackupSetName = "MyNorthwindBkp"
.BackupSetDescription = "Backup from VB.NET application"
oSQLServer.Connect(_INSTANCE, _USER, _PWD)
.SQLBackup(oSQLServer)
End With
MessageBox.Show("Backup Completed Sucessfully", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
I hope the steps are very simple. Specify the Backup file name, database name, backup set name, and some description. Connect using oSQLServer object; take the backup using the SQLBackup method of oBackup object by using the connection at oSQLServer object.
Similarly, we can have the code for Restore process as follows:
Private Sub doRestore()
With oRestore
'.Devices = "[NorthwindBackup]"
.Files = _BACKUPFILE
.Database = _DATABASE
.ReplaceDatabase = True
oSQLServer.Connect(_INSTANCE, _USER, _PWD)
.SQLRestore(oSQLServer)
End With
MessageBox.Show("Restore Completed Successfully", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
NOTE: Make sure you close the SQL Server Enterprise Manager before operating the Restore process, as it maintains an exclusive connection to the database. If you drop the existing database and try to restore it, you have no need to close it.
Remarks: The entire source code of this article exists in the form of zip downloadable (which includes the SQL-DMO object tree GIF). I developed this small tool using Microsoft SQL Server 2000 Enterprise Edition and Visual Studio.NET 2003 Enterprise Architect. It may also work with other SQL Server versions prior to MS SQL Server 2000, but don’t hesitate to drop in if the tool doesn’t work with other versions (basically I didn’t test it on other versions). As SQL-DMO is a COM component, you can also use it with Visual Basic 6.0 (but with very slight syntactical changes from the above code). You can also develop web based database administration tools using classic ASP or ASP.NET with SQL-DMO.
I leave it to the programmers for further enhancements. Any doubts, comments, suggestions, bugs, errors or feedback are welcomed at jag_chat@yahoo.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|