简体   繁体   English

使用所有备份集通过SMO还原数据库

[英]Use all backup sets to restore database with SMO

My problem is really simple. 我的问题很简单。 I have a .bak file that contains one or more backup set. 我有一个.bak文件,其中包含一个或多个备份集。

When I'm using SMO to restore the database with this .bak file, it only takes the first backup set to do its work. 当我使用SMO来通过此.bak文件还原数据库时,它仅需要第一个备份集来完成其工作。 It seems to ignore the remaining sets. 似乎忽略了其余的设置。

Why's that ? 为什么 ?

See my code : 看我的代码:

            //Sets the restore configuration
            Restore restore = new Restore()
            {
                Action = RestoreActionType.Database,
                Database = _databaseToRestore.DatabaseName,
                ReplaceDatabase = true
            };

            restore.Devices.Add(new BackupDeviceItem(_backupFilePath, DeviceType.File));

            Server server = new Server(_databaseToRestore.ServerName);

            DataTable fileList = restore.ReadFileList(server);
            string serverDataFolder = server.Settings.DefaultFile;

            if (string.IsNullOrEmpty(serverDataFolder))
                serverDataFolder = server.Information.MasterDBPath;

            foreach (DataRow file in fileList.Rows)
            {
                restore.RelocateFiles.Add(
                    new RelocateFile((string)file["LogicalName"],
                    Path.Combine(serverDataFolder, _databaseToRestore.DatabaseName + Path.GetExtension((string)file["PhysicalName"]))));
            }

            //Gets the exclusive access to database
            server.KillAllProcesses(_databaseToRestore.DatabaseName);
            restore.Wait();

            restore.SqlRestore(server);

I thought the BackupDeviceItem could gives me a feedback on how many backup sets there's inside, this way I could warn the user, but it's not. 我以为BackupDeviceItem可以给我有关内部有多少备份集的反馈,这样我就可以警告用户,但事实并非如此。

Anyone has a clue on this ? 有人对此有线索吗?

Thanks for your time. 谢谢你的时间。

Ok, fixed my problem. 好的,解决了我的问题。

The important field is FileNumber on the Restore object. 重要字段是Restore对象上的FileNumber The default value is 1, so that's why it always took my first backup set. 默认值为1,这就是为什么它总是采用我的第一个备份集的原因。

I just had to set this property to the number of backup sets in the file and now it takes the most recent backup done. 我只需要将此属性设置为文件中备份集的数量,现在就可以完成最新的备份。

Note : No differencial backups are implicated in this concern. 注意:此差异不涉及差异备份。

I just found out that I could easily know how many backup sets the file contains. 我刚刚发现,我可以轻松知道该文件包含多少备份集。

DataTable backupSets = restore.ReadBackupHeader(server);

Now, a simple backupSets.Rows.Count can help me to warn the user. 现在,一个简单的backupSets.Rows.Count可以帮助我警告用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM