C# 恢复数据库陷入“正在恢复...”状态

发布于 2024-12-05 15:13:08 字数 2405 浏览 1 评论 0原文

我正在将 SQL 数据库备份和恢复部分插入到我的程序中。我一直在使用 MSDN 示例(尝试了另一个,但不起作用)。 MSDN 页面 - http://msdn.microsoft.com/en-us/library/ ms162133.aspx

我已经使备份文件正常工作,并在 Management Studio 中测试了该文件。

但我在恢复文件时遇到了麻烦。

代码似乎可以工作,但是 SQL Server 中的数据库卡在“正在恢复...”中,

            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Server sqlServer = new Server();

                Restore sqlRestore = new Restore();
                sqlRestore.NoRecovery = true;
                sqlRestore.Action = RestoreActionType.Database;

                BackupDeviceItem deviceItem = new BackupDeviceItem(openFile.FileName, DeviceType.File);
                sqlRestore.Devices.Add(deviceItem);
                sqlRestore.Database = "firstRecoverTest";

                sqlRestore.SqlRestore(sqlServer);

                //Add the recovered database into the Entity Table
                SqlCommand intoEntity = new SqlCommand("IF NOT EXISTS(SELECT entityName FROM Entitys WHERE entityName = 'firstRecoveryTest') INSERT INTO Entitys VALUES ('firstRecoveryTest');", sqlConnection);                   
                sqlConnection.Open();
                intoEntity.ExecuteNonQuery();

                Database db = default(Database);
                db = sqlServer.Databases["firstRecoverTest"];
                db.RecoveryModel = (RecoveryModel)1;
                db.AutoClose = true;
                //db.Alter();

            }

在示例中有一个 db.Alter();函数,但这会抛出一个错误,提示“数据库'firstRecoverTest'的更改失败”。

请让我知道您的想法

提前致谢

更新

插入“ReplaceDatabase = true;”后最终结果没有变化。 还逐行浏览代码,表明它正在完成。

“db.Alter();”只是放在代码末尾的内容(显示为注释)。它用于创建备份并且工作不会出错。 使用 db.Alter(); 时,InnerError 显示此信息;

“当数据库处于正在恢复状态时,不允许 ALTER DATABASE”

有趣的部分是 SQL 日志文件。我收到 3 条日志:

“正在启动数据库‘firstRecoverTest’。”

“数据库‘firstRecoverTest’被标记为RESTORING,并且处于不允许运行恢复的状态。”

“数据库已恢复:数据库:firstRecoverTest,创建日期(时间):2011/09/20(15:44:48),第一个LSN:37:159:37,最后一个LSN:37:175:1,转储设备数量:1、设备信息:(FILE=1,TYPE=DISK:{'C:\Users\Administrator\Desktop\installer_backup'})。无需用户操作。”

但是,当我使用 SQL Management Studio 进行正常恢复时,还会有 2 个日志条目显示

“正在启动数据库 '[databaseName]'”。

“数据库 '[databaseName]' 上的恢复已完成。该数据库现在可用”

不幸的是,我没有足够的声誉来发布 SQL Management Studio 中的小图像。

I am inserting a SQL Database Backup and Recovery section into my program. I have been using MSDN Examples (tryed another and it wouldnt work).
MSDN Page - http://msdn.microsoft.com/en-us/library/ms162133.aspx

I have got the Backup file working, and have tested the file in Management Studio.

But I am having trouble with the Recovery of the file.

The code seems to be working, but the database in SQL Server is stuck in "Restoring..."

            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Server sqlServer = new Server();

                Restore sqlRestore = new Restore();
                sqlRestore.NoRecovery = true;
                sqlRestore.Action = RestoreActionType.Database;

                BackupDeviceItem deviceItem = new BackupDeviceItem(openFile.FileName, DeviceType.File);
                sqlRestore.Devices.Add(deviceItem);
                sqlRestore.Database = "firstRecoverTest";

                sqlRestore.SqlRestore(sqlServer);

                //Add the recovered database into the Entity Table
                SqlCommand intoEntity = new SqlCommand("IF NOT EXISTS(SELECT entityName FROM Entitys WHERE entityName = 'firstRecoveryTest') INSERT INTO Entitys VALUES ('firstRecoveryTest');", sqlConnection);                   
                sqlConnection.Open();
                intoEntity.ExecuteNonQuery();

                Database db = default(Database);
                db = sqlServer.Databases["firstRecoverTest"];
                db.RecoveryModel = (RecoveryModel)1;
                db.AutoClose = true;
                //db.Alter();

            }

In the example there is a db.Alter(); function, but that throws an error that says "Alter failed for Database 'firstRecoverTest'".

Please let me know your thoughts

Thanks in advance

UPDATE

After inserting the "ReplaceDatabase = true;" there was no change in the end result.
Also stepping though the code line by line, shows that it is making it through.

The "db.Alter();" is just that placed at the end of the code (shown as comment). It is used in the creation of the backup and works without error.
InnerError shows this information when using db.Alter();

"ALTER DATABASE is not permitted while a database is in the Restoring state"

The interesting part is the SQL Log files. I am getting 3 Logs:

"Starting up database 'firstRecoverTest'."

"The database 'firstRecoverTest' is marked RESTORING and is in a state that does not allow recovery to be run."

"Database was restored: Database: firstRecoverTest, creation date(time): 2011/09/20(15:44:48), first LSN: 37:159:37, last LSN: 37:175:1, number of dump devices: 1, device information: (FILE=1, TYPE=DISK: {'C:\Users\Administrator\Desktop\installer_backup'}). Informational message. No user action required."

However, when I do a normal recover using SQL Management Studio there is 2 more log entrys saying

"Starting up database '[databaseName]'."

"Restore is complete on database '[databaseName]'. The database is now available"

I don't have enough reputation to post a small image of how it is in SQL Management Studio unfortunatly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

要走干脆点 2024-12-12 15:13:08

您应该尝试删除数据库或使用sqlRestore.ReplaceDatabase = true;

http://msdn.microsoft.com /en-us/library/microsoft.sqlserver.management.smo.restore.replacedatabase.aspx

如果看起来没有发生任何事情,您可以在单独的线程中启动该进程并连接事件以通知进度更改通过使用这些。

sqlRestore.Complete += new ServerMessageEventHandler(completionEvent);
sqlRestore.PercentCompleteNotification = 10; // Call progress event every x percent change.
sqlRestore.PercentComplete += new PercentCompleteEventHandler(progressEvent);

如果这不起作用,您可以发布不起作用的更改代码吗?还要检查 SQL Server 日志和权限。

已更新

好吧,更新更有意义。原因是您正在设置sqlRestore.NoRecovery = true;。这样做的效果是,恢复完成后,数据库将保持在恢复状态,因此除了刚刚恢复的完整备份之外,您还可以恢复额外的差异备份。

当它处于这种状态时,您将无法对数据库执行任何操作。我建议除非您需要它,否则请保留默认值 NoRecovery = false

请参阅 http://msdn.microsoft。 com/en-us/library/microsoft.sqlserver.management.smo.backuprestorebase.norecovery.aspx

希望有所帮助。

You should try either dropping the database or using sqlRestore.ReplaceDatabase = true;.

http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.restore.replacedatabase.aspx

If it looks like nothing is happening you can start the process in a seperate thread and wire up the events for notification of progress changes by using these.

sqlRestore.Complete += new ServerMessageEventHandler(completionEvent);
sqlRestore.PercentCompleteNotification = 10; // Call progress event every x percent change.
sqlRestore.PercentComplete += new PercentCompleteEventHandler(progressEvent);

If that doesn't work can you please post the Alter code that wasn't working. Also check SQL server logs and permissions.

UPDATED

Ok that update makes more sense. The reason is because you are setting sqlRestore.NoRecovery = true;. This has the effect that after the restore is done the DB is kept in a recovery state so you can restore an additional differential backup in addition to the full you just restored.

While it is in this state you will not be able to do anything more to the database. I would suggest that unless you require it you leave the default which is NoRecovery = false.

See http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.backuprestorebase.norecovery.aspx

Hope that helps.

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