gpt4 book ai didi

c# - 无法将数据库还原到 SQL Server 中的快照

转载 作者:行者123 更新时间:2023-11-30 15:21:46 25 4
gpt4 key购买 nike

我想创建一个数据库快照,并在每次运行单元测试时将数据库恢复到它。我能够创建快照,但在恢复它时遇到以下错误。

Msg 5070, Level 16, State 2, Line 1
Database state cannot be changed while other users are using the database 'ImportData'

Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

下面列出了用于创建数据库和将数据库恢复为快照的 SQL 查询。

创建快照:

CREATE DATABASE Data_SShot
ON (NAME=Data,
FILENAME='C:\Snapshot\DataSnapshot.ss'),
(NAME=Data_Data1,
FILENAME='C:\Snapshot\Data1Snapshot.ss'),
(NAME=Data_Index1,
FILENAME='C:\Snapshot\DataIndexSnapshot.ss')
AS SNAPSHOT OF Data

恢复到快照

use master
go

RESTORE DATABASE Data
FROM DATABASE_SNAPSHOT = 'Data_SShot'

它指出

Database state cannot be changed while other users are using the database

如何克服这个问题?我正在使用 .NET (C#) 来执行此操作。如何关闭连接以成功运行 RESTORE DATABASE

我希望创建快照并将数据库恢复到它的整个过程在测试套件中的每个测试中都发生。

最佳答案

在恢复数据库之前需要做的事情很少。

  1. Get the number of active users for the DB and kill the sessions.
  2. Take the DB offline and online which will terminate all hidden connections.
  3. Now restore the DB. If it fails, then take the DB offline and try to restore.
  4. Once restore is done, bring the DB online.

这些是我一直用于数据库恢复的步骤,它对我有用了很长时间。

关于c# - 无法将数据库还原到 SQL Server 中的快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36789860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com