从 bak 文件中检索数据库名称

发布于 2024-10-02 07:41:42 字数 127 浏览 5 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

冬天旳寂寞 2024-10-09 07:41:43
RESTORE FILELISTONLY
FROM DISK = 'full path to your .bak file'

将显示备份中当前的文件名。如果一个文件中有多个备份,并且您没有指定“WITH FILE = X”,您将仅获得文件中第一个备份的信息。

RESTORE DATABASE MyNewDBname
    FROM DISK = 'full path to your .bak file'
    WITH 
      MOVE 'LogicalFilename_Data' TO 'D:\somepath\...\MyDB.mdf',
      MOVE 'LogicalFilename_Log' TO 'D:\somepath\...\MyDB.ldf';
GO

SMO 的粗略轮廓(未经测试):

Restore restoreDB = new Restore();
restoreDB.Database = myDatabase.Name;
// Specify whether you want to restore database, files or log
restoreDB.Action = RestoreActionType.Database;
restoreDB.Devices.AddDevice(@"D:\somepath\...\MyDBFull.bak", DeviceType.File);

restoreDB.ReplaceDatabase = true; // will overwrite any existing DB     
restoreDB.NoRecovery = true;

// you can Wire up events for progress monitoring */
// restoreDB.PercentComplete += CompletionStatus;
// restoreDB.Complete += RestoreCompleted;

restoreDB.SqlRestore(myServer);

参考

使用 SMO,您可以使用 Restore.ReadFileList

另请参阅:如何从C#

RESTORE FILELISTONLY
FROM DISK = 'full path to your .bak file'

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.

RESTORE DATABASE MyNewDBname
    FROM DISK = 'full path to your .bak file'
    WITH 
      MOVE 'LogicalFilename_Data' TO 'D:\somepath\...\MyDB.mdf',
      MOVE 'LogicalFilename_Log' TO 'D:\somepath\...\MyDB.ldf';
GO

Rough outline with SMO (not tested):

Restore restoreDB = new Restore();
restoreDB.Database = myDatabase.Name;
// Specify whether you want to restore database, files or log
restoreDB.Action = RestoreActionType.Database;
restoreDB.Devices.AddDevice(@"D:\somepath\...\MyDBFull.bak", DeviceType.File);

restoreDB.ReplaceDatabase = true; // will overwrite any existing DB     
restoreDB.NoRecovery = true;

// you can Wire up events for progress monitoring */
// restoreDB.PercentComplete += CompletionStatus;
// restoreDB.Complete += RestoreCompleted;

restoreDB.SqlRestore(myServer);

Ref.

Using SMO, you can retrieve the file list using Restore.ReadFileList

See Also: How to restore a database from C#

携君以终年 2024-10-09 07:41:43

我能够以一种非常黑客且不确定的方式获取数据库的名称。然而,这确实对我有用,而且我只是在一个小型测试和开发工具中为自己使用它。

如果您遵循与 Mitch Wheat 相同的答案,则需要执行以下操作:

之后

restoreDB.Devices.AddDevice(@"D:\somepath\...\MyDBFull.bak", DeviceType.File);

在添加以下三行

var fileList = restoreDB.ReadFileList(myServer);

string databaseName = fileList.Rows[0].ItemArray[0].ToString();

restoreDB.Database = databaseName;

:您使用 myServer 读取关联的文件列表,并从 myServer 获取信息>DataTableItemArray。该名称似乎出现在第一行和项目数组的第一项中。之后,使用此名称设置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

restoreDB.Devices.AddDevice(@"D:\somepath\...\MyDBFull.bak", DeviceType.File);

Add these three lines:

var fileList = restoreDB.ReadFileList(myServer);

string databaseName = fileList.Rows[0].ItemArray[0].ToString();

restoreDB.Database = databaseName;

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!

我的鱼塘能养鲲 2024-10-09 07:41:43

按如下方式尝试:

DECLARE @BackUpPath nvarchar(2000)='X:\YourBackUpFileName.bak'
EXEC('RESTORE FILELISTONLY  FROM DISK=''' +@BackUpPath+ '''  ')

,X 是包含备份文件的驱动器。

JUST Try as Follow:

DECLARE @BackUpPath nvarchar(2000)='X:\YourBackUpFileName.bak'
EXEC('RESTORE FILELISTONLY  FROM DISK=''' +@BackUpPath+ '''  ')

and X is your drive that contains backup file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文