使用所有备份集以使用SMO还原数据库 [英] Use all backup sets to restore database with SMO

查看:473
本文介绍了使用所有备份集以使用SMO还原数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是非常简单的。
我有一个包含一个或多个备份集的.bak文件。

My problem is really simple. I have a .bak file that contains one or more backup set.

当我使用SMO恢复与此.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. It seems to ignore the remaining sets.

为什么这么

请参阅我的代码:

            //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);



我以为BackupDeviceItem可以给我多少备份集里面有一个反馈,这样我可以向用户发出警告,但它不是。

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.

任何人有这个?

感谢您的时间线索。

推荐答案

好吧,固定我的问题。

重要的领域是 FileNumber 恢复对象。
中的默认值是1,所以这就是为什么它总是拉着我的第一个备份集。

The important field is FileNumber on the Restore object. The default value is 1, so that's why it always took my first backup set.

我不得不将此属性设置为备份集的数量该文件,现在需要做的最新的备份

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.

这篇关于使用所有备份集以使用SMO还原数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆