Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

SQL Server Backup/Restore Through SQLDMO and C#
By Kevin Matthew Goss

Recently I had a need to be able to backup and restore databases from c#. I referred to the SQLDMO object and found it to be quite simple. The sample code will show you a simple method for doing backup/restore of SQL Server databases on a local network. I say "simple" because there are more complex and detailed methods for performing these operations, such as when you are using a backup device that is not a file. You can use these methods quite easily as well, but for 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 the reference to the COM object for you, but if not, just reference it as you see below.

The sample project does a few things. First it gets a list of available SQL Servers on the network on the Form_Load event and populates a combobox with the choices. You enter a login name and a password (SQL Authentication) and click "Connect" to get a list of the databases on the server. Once you have done this you can type a file name in or select one from the dialog. Please keep in mind that if it is a remote server, the dialogs will only point to a local file location which may not exist remotely. The backups and restores work based on physical paths relative to the server the database resides on. For instance, if it is a remote server, and the file path is c:\backup\db.bak, but the directory c:\backup does not exist on the remote server, it will fail. Althought this is a very base example, it has been useful to me. Happy coding!!!

Download SQLBackupRestore.zip