c# - 使用所有备份集通过 SMO 恢复数据库

标签 c# sql-server database restore smo

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

当我使用 SMO 通过这个 .bak 文件恢复数据库时,它只需要第一个备份集就可以完成它的工作。它似乎忽略了剩余的集合。

这是为什么?

查看我的代码:

            //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 可以给我一个关于里面有多少备份集的反馈,这样我就可以警告用户,但事实并非如此。

有人对此有线索吗?

感谢您的宝贵时间。

最佳答案

好的,解决了我的问题。

重要的字段是 Restore 对象上的 FileNumber。 默认值为 1,所以这就是为什么它总是占用我的第一个备份集。

我只需要将此属性设置为文件中备份集的数量,现在它会完成最新的备份。

注意:此问题不涉及差异备份。

关于c# - 使用所有备份集通过 SMO 恢复数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3094609/

相关文章:

c# - 在当前 docx 文件上执行 word 加载项

sql-server - 当存储过程正在运行时,如何使用PRINT语句跟踪执行?

SQL - 从井号 (#) 符号后的字符串中获取数字

php - 处理没有 mysql 的主机

database - 如果使用 sorm 如何处理架构更改

c# - Java 枚举与 C# 枚举 - 缺少的功能

c# - Windows Azure 中的 APNS

c# - 如何预加载 WPF 运行时引擎或如何减少其加载时间?

sql - 根据 SQL 函数中的顺序对列进行排序

mysql - 在两个 MySQL 数据库之间传输数据,无需进行不必要的架构更改