使用 SMO 更改逻辑数据库名称
使用 SMO 恢复数据库时如何更改逻辑数据库名称?
/维克托
How can I change the logical database name when restoring a database with SMO?
/Viktor
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
使用 SMO 恢复数据库时如何更改逻辑数据库名称?
/维克托
How can I change the logical database name when restoring a database with SMO?
/Viktor
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
您无法使用 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.
Rahul 的代码是正确的:恢复到新的物理文件并重命名逻辑文件是一个两步过程:
RelocateFile
调用表示“将此逻辑文件名映射到此物理文件”。您需要在此处使用原始备份的逻辑文件名,而不是新的逻辑文件名,否则您可能会遇到“.mdf 无法覆盖
”异常。要创建新的逻辑名称,请随后使用
Rename()
调用,如 Rahul 的代码所示。但是,如果您想使用 SMO 更改数据库的名称:
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: