如何确保使用SMO恢复Alla数据?

时间:2019-07-03 09:51:25

标签: sql-server restore smo

当原始数据库处于SIMPLE恢复模式时,使用SMO对象使用C#代码执行的恢复可以很好地恢复。我们遇到了一个数据库的问题,该数据库的还原操作丢失了某个表中的所有内容,该表中的所有数据都在后期插入。数据库显示处于“批量记录”恢复模式。更改为SIMPLE并执行新的备份后,使用我们的代码,它恢复正常。

我们在还原对象上尝试了不同的设置,但没有一个可以解决该问题。我们的印象是,还原会忽略日志中的数据。

基本还原如下:

sqlServer = new Server(new ServerConnection(instanceName));
restore = GetRestore();
restore.PercentComplete += PercentCompleteAction;
restore.Complete += CompleteAction;
restore.SqlRestore(sqlServer);

GetRestore函数基本上是这样实现的:

restore = new Restore();
var deviceItem = new BackupDeviceItem(backupFileName, DeviceType.File);
restore.Devices.Add(deviceItem);
restore.Database = newDatabaseName;
restore.NoRecovery = false;
restore.Action = RestoreActionType.Database;
restore.ReplaceDatabase = false;
return restore;

没有错误消息-只是一个表中缺少内容。

添加了尝试: 我对下面的解决方案进行了猜测,但这没有帮助:

restore.ReplaceDatabase = false;
restore.NoRecovery = true;
restore.Action = RestoreActionType.Database;
restore.SqlRestore(sqlServer);
restore.ReplaceDatabase = true;
restore.NoRecovery = true;
restore.Action = RestoreActionType.Log;
restore.SqlRestore(sqlServer);
restore.ReplaceDatabase = true;
restore.NoRecovery = false;
restore.Action = RestoreActionType.Files;
restore.SqlRestore(sqlServer);

1 个答案:

答案 0 :(得分:0)

你需要在你的日志文件中选择一个正确的文件编号,看看这个方法可以解决你的问题:

public static void restaurarBackup(string pathFileBak)
    {
        SqlConnection conn = new SqlConnection(Connection.getCon());
        Server smoServer = new Server(new ServerConnection(conn));

        string localFilePath = pathFileBak;
        string db_name = "ETrade";
        string defaultFolderEtrade = @"C:\ETrade\";
        Restore rs = new Restore();
        rs.NoRecovery = false;
        rs.ReplaceDatabase = true;
        BackupDeviceItem bdi = default(BackupDeviceItem);
        bdi = new BackupDeviceItem(localFilePath, DeviceType.File);
        rs.Devices.Add(bdi);
        DataTable dt = rs.ReadFileList(smoServer);
        foreach (DataRow r in dt.Rows)
        {
            string logicalFilename = r.ItemArray[dt.Columns["LogicalName"].Ordinal].ToString();
            string physicalFilename = defaultFolderEtrade + Path.GetFileName(r.ItemArray[dt.Columns["PhysicalName"].Ordinal].ToString());
            rs.RelocateFiles.Add(new RelocateFile(logicalFilename, physicalFilename));
        }
        DataTable backupHeaders = rs.ReadBackupHeader(smoServer);
        rs.FileNumber = Convert.ToInt32(backupHeaders.AsEnumerable().Max(backupInfo => backupInfo["Position"]));
        rs.Database = db_name;
        smoServer.KillAllProcesses(rs.Database);
        Microsoft.SqlServer.Management.Smo.Database db = smoServer.Databases[rs.Database];
        db.DatabaseOptions.UserAccess = DatabaseUserAccess.Single;
        rs.SqlRestore(smoServer);

        db = smoServer.Databases[rs.Database];
        db.SetOnline();
        smoServer.Refresh();
        db.Refresh();
    }

如果您想在日志文件中查看备份,see this query

SELECT database_name, name, backup_start_date, 
backup_finish_date, datediff(mi, backup_start_date, backup_finish_date) [tempo (min)],
 position, first_lsn, last_lsn, server_name, recovery_model, 
 type, cast(backup_size/1024/1024 as numeric(15,2)) [Tamanho (MB)], B.is_copy_only
FROM msdb.dbo.backupset B