无法将数据库还原到 SQL 服务器中的快照
Unable to restore database to a snapshot in SQL Server
我想在每次单元测试 运行 时创建一个数据库快照并将数据库恢复到它。我能够创建快照,但在恢复它时遇到以下错误。
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
的连接?
我希望创建快照并将数据库恢复到它的整个过程在测试套件中的每个测试中都发生。
在恢复数据库之前需要做的事情很少。
- Get the number of active users for the DB and kill the sessions.
- Take the DB offline and online which will terminate all hidden connections.
- Now restore the DB. If it fails, then take the DB offline and try to restore.
- Once restore is done, bring the DB online.
这些是我一直用于数据库还原的步骤,它对我有用了很长时间。
执行以下操作:
ALTER DATABASE [Data] SET SINGLE_USER
RESTORE DATABASE Data
FROM DATABASE_SNAPSHOT = 'Data_SShot'
ALTER DATABASE [Data] SET MULTI_USER WITH NO_WAIT
我想在每次单元测试 运行 时创建一个数据库快照并将数据库恢复到它。我能够创建快照,但在恢复它时遇到以下错误。
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
的连接?
我希望创建快照并将数据库恢复到它的整个过程在测试套件中的每个测试中都发生。
在恢复数据库之前需要做的事情很少。
- Get the number of active users for the DB and kill the sessions.
- Take the DB offline and online which will terminate all hidden connections.
- Now restore the DB. If it fails, then take the DB offline and try to restore.
- Once restore is done, bring the DB online.
这些是我一直用于数据库还原的步骤,它对我有用了很长时间。
执行以下操作:
ALTER DATABASE [Data] SET SINGLE_USER
RESTORE DATABASE Data
FROM DATABASE_SNAPSHOT = 'Data_SShot'
ALTER DATABASE [Data] SET MULTI_USER WITH NO_WAIT