How to change the name of the target database when performing the restore? (Class Restore C # .NET)

2

I am backing up and restoring a SQL Server database using the C # .NET Backup and Restore classes, with Smo. The problem is that after performing the backup of the database with the name, for example "Test", I can not restore the database with another name, which is possible using tools such as Management Studio. Is it possible to restore the database with another name through the class Restore?

public void Restore(String dataBaseName, pathFile)
    {
        bdi = default(BackupDeviceItem);
        bdi = new BackupDeviceItem(pathFile, DeviceType.File);           

        Restore rs = new Restore();

        rs.NoRecovery = false;

        rs.SqlVerify(srv);        

        rs.Devices.Add(bdi);

        rs.Database = dataBaseName;

        rs.SqlRestore(srv);

        rs.Devices.Remove(bdi);
    }
    
asked by anonymous 20.01.2015 / 12:45

1 answer

2

According to this answer in StackOverflow in English, you should do the following:

  • Assign the desired new name to the bank in the Restore.Database property.
  • Assign the value true to the property Restore.ReplaceDatabase .
  • Specify new data files and logs in the property Restore.RelocateFiles .
  • 24.07.2015 / 18:27