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

发布于 2024-08-22 10:28:40 字数 46 浏览 2 评论 0原文

使用 SMO 恢复数据库时如何更改逻辑数据库名称?

/维克托

How can I change the logical database name when restoring a database with SMO?

/Viktor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往昔成烟 2024-08-29 10:28:40
//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");
//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");
夏の忆 2024-08-29 10:28:40

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

您可以使用ALTER DATABASE 通常在 SQL 中。

这似乎已得到 RelocateFile SMO 类。

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.

雪化雨蝶 2024-08-29 10:28:40

Rahul 的代码是正确的:恢复到新的物理文件并重命名逻辑文件是一个两步过程:

RelocateFile 调用表示“将此逻辑文件名映射到此物理文件”。您需要在此处使用原始备份的逻辑文件名,而不是新的逻辑文件名,否则您可能会遇到“.mdf 无法覆盖”异常。

要创建新的逻辑名称,请随后使用 Rename() 调用,如 Rahul 的代码所示。

但是,如果您想使用 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();

Rahul's code is correct: Restoring to new physical files and renaming logical files is a two-step process:

The RelocateFile call is saying "map this logical file name to this physical file". You need to use the logical file names of the original backup here NOT new ones, otherwise you are likely to get ".mdf cannot be overwritten" exceptions.

To make new logical names, use the Rename() calls afterwards, as shown in Rahul's code.

However, if you want to change the name of the database 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文