SQL 服务器应用程序 - 将数据库重置为原始状态

SQL Server Application - Resetting database to original state

背景

我需要用 C#(大约 120 个)为 C#/SQL 服务器应用程序编写一些集成测试。现在,最初在任何测试之前,数据库已经存在,它存在的原因是因为很多脚本将 运行 设置它(大约 20 分钟 运行ning 时间)。现在,当我 运行 我的测试时,将更新很少的表(CRUD 操作)。例如在 10-11 个表中,将添加几行,在 15-16 个表中,将更新几行,在 4-5 个表中,将删除几行。

问题

After 每个测试都是 运行,the database 需要 reset 到它的原始状态。我怎样才能做到这一点?

错误的解决方案

每 运行 次测试后,re-run 数据库 creation scripts(20 分钟 运行ning 时间)。由于将进行大约 120 次测试,因此需要 40 个小时,这不是一个选择。其次,有一个进程针对该数据库打开了多个连接,因此数据库不能 dropped/re-created.

好的解决方案?

请问有没有其他方法可以解决这个问题?我遇到的另一个问题是,对于这些测试中的每一个,我什至不知道将更新哪些表,如果我要删除,我将不得不手动检查以查看更新了哪些表,将数据库恢复为它是通过编写查询手动设置的原始状态。

您应该看看 MSSQl 为您提供的拍摄数据库快照的可能性。它可能比恢复备份或重新创建数据库快得多。

Managing a test database

In a testing environment, it can be useful when repeatedly running a test protocol for the database to contain identical data at the start of each round of testing. Before running the first round, an application developer or tester can create a database snapshot on the test database. After each test run, the database can be quickly returned to its prior state by reverting the database snapshot.