How can I change the logical database name when restoring a database with SMO?
/Viktor
//restore is the Restore object in SMO
restore.RelocateFiles.Add(new RelocateFile(SourceDataFile.Name, Path.Combine(destinationDirectory, destination.Database + ".mdf")));
restore.RelocateFiles.Add(new RelocateFile(SourceLogFile.Name, Path.Combine(destinationDirectory, destination.Database + "_log.ldf")));
restore.SqlRestore(destinationServer);
var destinationDatabase = destinationServer.Databases[destinationDatabaseName];
//renaming the logical files does the trick
destinationDatabase.FileGroups[0].Files[0].Rename(destinationDatabaseName);
destinationDatabase.LogFiles[0].Rename(destinationDatabaseName + "_log");
You can't rename the logical database files with a SQL RESTORE DATABASE: it's not offered. Only physical files can be changed using WITH MOVE
You rename logical files by using ALTER DATABASE in SQL, normally.
This appears to be be confirmed by the RelocateFile SMO class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With