SQL Server Backup/Restore Through SQLDMO and C#

Recently I had a need to be able to backup andrestore databases from c#. I referred to the SQLDMO object and found itto be quite simple. The sample code will show you a simple method fordoing backup/restore of SQL Server databases on a local network. I say"simple" because there are more complex and detailed methods forperforming these operations, such as when you are using a backup devicethat is not a file. You can use these methods quite easily as well, butfor this article I will only use a file path to backup and restore to.To use the sample app, just open the project. It should fix thereference to the COM object for you, but if not, just reference it asyou see below.
The sample project does a few things. First itgets a list of available SQL Servers on the network on the Form_Loadevent and populates a combobox with the choices. You enter a login nameand a password (SQL Authentication) and click "Connect" to get a listof the databases on the server. Once you have done this you can type afile name in or select one from the dialog. Please keep in mind that ifit is a remote server, the dialogs will only point to a local filelocation which may not exist remotely. The backups and restores workbased on physical paths relative to the server the database resides on.For instance, if it is a remote server, and the file path isc:\backup\db.bak, but the directory c:\backup does not exist on theremote server, it will fail. Althought this is a very base example, ithas been useful to me. Happy coding!!!

Download SQLBackupRestore.zip












No comments yet... Be the first to leave a reply!