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

Asked

Viewed 1,272 times

2

I am backing up and re-storing an SQL Server database using the C# NET Backup and Restore classes with Smo.
The problem is that after backing up the database with the name, for example "Test", I cannot restore the database with another name, which is possible using tools such as Management Studio. It would be possible to restore the database with another name through the Restore class?

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);
    }
  • You can put an example of your code in your question?

  • Try changing the database name when backing up, not Restore. If it works, I put it in answer.

  • I thought about doing this, but if I put in time to take the backup it will not find the bank to copy.

  • I’ve done it but not with the Restore class but with Sqlcommand and sqlDataAdapt running an SQL to restore.

1 answer

2


According to that answer in Stackoverflow in English, you should do the following:

  1. Assign the desired new name to the bank in the Property Restore.Database.
  2. Assign true value to Property Restore.ReplaceDatabase.
  3. Specify new data files and logs in the Property Restore.RelocateFiles.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.