I\'m looking for a way to create a backup of a SQL Server 2008 database in C# code. I then wish to re-import this database to a local database server, again using C# code. <
What you want to do is have a couple of scripts that you run against the master database from C# using the regular connection objects (SqlConnection
, etc).
There are commands to backup and commands to restore to and from a file. All you then need to do is manage the file in your application.
To restore:
http://blog.sqlauthority.com/2007/02/25/sql-server-restore-database-backup-using-sql-script-t-sql/
To backup:
http://msdn.microsoft.com/en-us/library/ms191304.aspx
There is also a set of DLLs from Microsoft to work against SQL Server databases that are not part of the ADO.NET provider set. Here is a tutorial covering these:
http://www.codeproject.com/KB/database/SQL_Server_2005_Database.aspx