The correct Rahul code: restoring new physical files and renaming logical files is a two-step process:
The RelocateFile call says, "Copy this logical file name into this physical file." You must use the logical file names of the original backup here NOT new, otherwise you will probably get " .mdf cannot be overwritten " exceptions.
To create new logical names, use the Rename() calls after that, as shown in the Rahul code.
However, if you want to change the database name using SMO:
var srvConn = new ServerConnection(serverName) { LoginSecure = false, Login = dbUserName, Password = dbUserPassword, DatabaseName = "master", }; var mainDb = new Database(srvConn, "old database name"); mainDb.Rename("new database name"); mainDb.Refresh();
hillstuk
source share