Recover database from .bak file with new name

I sing C #, .NET 3.5 and SMO to create a copy of the database (our production database) in some other databases (our DEV and test database) in SQL Server 2005.

I managed to create a backup from the database to restore it. this, of course, overwrites the existing one. I used this CodeProject guide .

How to restore a database backup in another database with a different name on the same server?

+4
source share
2 answers

In short:

  • set the Restore.Database property Restore.Database new name,
  • set the Restore.ReplaceDatabase property to true ,
  • specify new data and log files using the Restore.RelocateFiles property.

For a detailed explanation, see Restoring a Database under New Location. Getting Started with SMO in SQL 2005 - Restores an article .

+5
source
 using Microsoft.SqlServer.Management.Smo; string backupFile="Backupfile";//.bak file string dbName = "DBName"; string dbServerMachineName = "MachineName"; Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server(dbServerMachineName); Database database = new Database(server, dbName); //If Need database.Create(); database.Refresh(); //Restoring Restore restore = new Restore(); restore.NoRecovery = false; restore.Action = RestoreActionType.Database; BackupDeviceItem bdi = default(BackupDeviceItem); bdi = new BackupDeviceItem(backupFile, DeviceType.File); restore.Devices.Add(bdi); restore.Database = dbName; restore.ReplaceDatabase = true; restore.PercentCompleteNotification = 10; restore.SqlRestore(server); database.Refresh(); database.SetOnline(); server.Refresh(); 
+7
source

All Articles