从 bak 文件中检索数据库名称
我有一个 bak 文件,其中包含数据库的备份。
我想将此数据库恢复到新位置,并且需要从此文件中检索数据库名称,知道如何执行此操作吗?
我需要它来覆盖数据文件位置和日志文件位置。
感谢您的帮助。
I have a bak file that has in it a back up of a database .
I want to restore this database to a new location and I need to retrieve the database name from this file any idea how to do so ?
I need it to override the data file location and the log file location .
Thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将显示备份中当前的文件名。如果一个文件中有多个备份,并且您没有指定“
WITH FILE = X
”,您将仅获得文件中第一个备份的信息。SMO 的粗略轮廓(未经测试):
参考。
使用 SMO,您可以使用 Restore.ReadFileList
另请参阅:如何从C#
will show you the current file names in the backup. If there are multiple backups in one file and you do not specify "
WITH FILE = X
" you will only get information for the first backup in the file.Rough outline with SMO (not tested):
Ref.
Using SMO, you can retrieve the file list using Restore.ReadFileList
See Also: How to restore a database from C#
我能够以一种非常黑客且不确定的方式获取数据库的名称。然而,这确实对我有用,而且我只是在一个小型测试和开发工具中为自己使用它。
如果您遵循与 Mitch Wheat 相同的答案,则需要执行以下操作:
之后
在添加以下三行
:您使用 myServer 读取关联的文件列表,并从 myServer 获取信息>DataTable 和 ItemArray。该名称似乎出现在第一行和项目数组的第一项中。之后,使用此名称设置restore对象的Database属性。瞧,如果您只是想恢复或在您不记得名称的现有数据库之间进行切换,则无需提供您自己的数据库名称。
警告:仅当您的数据库是在还原数据库的同一实例中创建时,此操作才有效。如果您使用此方法来恢复从其他地方创建和备份的数据库,它将不起作用,因为您可能需要设置更多属性!
您可能会问哪些属性?您必须亲自前往 ItemArray 中购物。然而,属性之一是数据库需要恢复到的位置。您还可以在那里找到位置。说明显而易见的事情,但请确保您的计算机上也存在用于驻留数据库的文件夹!
不管怎样,我用几个数据库尝试了这个,这对我有用,希望它对任何人都有帮助!
I was able to get the name of the database in what feels like a really hacky and uncertain way. However, this really works for me and I am just using this in a small test and development tool just for myself anyway.
If you follow the same answer as Mitch Wheat, what you need to do is the following:
Right after
Add these three lines:
You read the associated file list using myServer and you get the information from a DataTable and an ItemArray. The name seems to be present in the first row and in the first item in the item array. After that, use this name to set the Database property of the restore object. Voilà, no need to provide your own database name if you simply want to restore or switch between existing databases whose names you don't really remember any more.
WARNING: This works only if your database has been created in the same instance in which you're restoring it. If you're using this method to restore databases created and backed up from elsewhere it will not work because there are more properties you will probably need to set!
Which properties you might ask? You will have to go and shop for yourself inside the ItemArray. Nevertheless, one of the properties is the location to which the database needs to be restored. You can also find the location in there. Stating the obvious but make sure the folder in which the database used to reside also exists on your computer!
Anyway, I tried this out with a couple of databases and this works for me and hope it helps anybody out there!
按如下方式尝试:
,X 是包含备份文件的驱动器。
JUST Try as Follow:
and X is your drive that contains backup file.