简体   繁体   English

以编程方式从生产数据库创建空白数据库

[英]create a blank database from a production database programmatically

For software testing purposes I would like to create a sterile clone (with all data blanked out) of the production database. 为了进行软件测试,我想创建一个生产数据库的无菌克隆(所有数据都被清空)。 This way I can run my unit tests on a known set of records every time. 这样,我可以每次对一组已知记录运行单元测试。 I am looking to try and do this programmatically within the unit tests themselves so I can ensure that the tables contain exactly the test data that I need for the functional tests. 我正在尝试尝试在单元测试本身中以编程方式进行此操作,因此我可以确保表格完全包含功能测试所需的测试数据。

I have found the following information relating to creating an Access database within C#. 我发现以下有关在C#中创建Access数据库的信息 Note: I know Access probably isn't the best solution, but its good enough! 注意:我知道Access可能不是最好的解决方案,但是已经足够好了!

What I would like to know, is there a way of using TableAdapters (perhaps) to replicate the production database schema (without any data) within a blank Access database file? 我想知道的是,有没有一种方法可以使用TableAdapters(也许)在空白的Access数据库文件中复制生产数据库架构(不包含任何数据)?

Do this: 做这个:

  • create a copy of the access file; 创建访问文件的副本; production -> test 生产->测试
  • connect to test database 连接到测试数据库
  • enumerate all tables in the database 枚举数据库中的所有表
  • run DELETE * FROM [table] for all tables. 对所有表运行DELETE * FROM [table] run it several times if you have FK dependencies until there is no error - or TRUNCATE [table] as commented 如果您有FK依赖项,请运行几次,直到没有错误-或按注释所示TRUNCATE [table]
  • compact the database 压缩数据库

I do not have much experience with Access, but generally you would make a CREATE script for this purpose. 我在Access方面经验不足,但是通常您会为此创建一个CREATE脚本。 Most database tools have a function for creating such a script. 大多数数据库工具都具有创建此类脚本的功能。 Such a script basically is a set of SQL statements that create all the objects (eg databases, views). 这样的脚本基本上是一组创建所有对象(例如数据库,视图)的SQL语句。

Searching for CREATE script and Access will give you some starting points . 搜索CREATE脚本和Access将为您提供一些起点

I have bad experiences with Access as a production database. 我对Access作为生产数据库有不好的经验。 I won't recommend. 我不推荐。 Either go with SQLite or Firebird. 要么使用SQLite要么使用Firebird。

Secondly, yes you can use TableAdapters. 其次,是的,您可以使用TableAdapters。 You need to create two connections for each db. 您需要为每个数据库创建两个连接。 But I think there might be tools available to do this. 但是我认为可能有工具可以做到这一点。

Edited ** 编辑**

How big is the database? 数据库有多大? For up to 4GB, Oracle Express Edition might help. 对于高达4GB的空间,Oracle Express Edition可能会有所帮助。 Also, it will be easy to clone from Oracle to Oracle. 同样,从Oracle克隆到Oracle也很容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM