使用SMO更改逻辑数据库名称

Vik*_*tor 8 c# sql-server smo

在使用SMO还原数据库时,如何更改逻辑数据库名称?

/维克托

Rah*_*hul 10

//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");
Run Code Online (Sandbox Code Playgroud)


gbn*_*gbn 5

您不能使用SQL RESTORE DATABASE重命名逻辑数据库文件:未提供。使用WITH MOVE只能更改物理文件

通常,可以使用SQL中的ALTER DATABASE重命名逻辑文件。

这似乎已由RelocateFile SMO类确认。