以编程方式重新创建现有的 SQL Server 数据库?
是否可以复制现有数据库,或者生成 sql,然后针对 sql 数据库运行它以创建现有数据库的副本?
我不关心数据,只关心模式。
我想为单元测试执行此操作。我知道我可以只使用 sqlite,但我很好奇它是如何工作的。
我没有任何存储过程或任何东西,只是表和 PK/FK 等。
我想最好的方法是以某种方式生成 sql 模式,然后针对数据库运行它。
或者,如果更容易的话,复制数据库然后截断表。
注意:我想知道如何以编程方式完成此操作。
Is it possible to either copy and existing database, or generate the sql and then run it against a sql database to create a copy of an existing database?
I don't care about the data, just the schema.
I want to do this for unit testing. I know I can just use sqlite, but I am curious how this would work anyhow.
I don't have any stored procs or anything, simply tables and PK/FK etc.
I guess the best approach would be somehow to generate the sql schema, then just run it against the database.
Or if it is easier, copy the database and then truncate the tables.
Note: I want to know how this can be done programatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过右键单击数据库->生成脚本任务->从 sql Management Studio 生成脚本。

在向导中,您可以在高级设置(取决于 sql 管理工具版本)下选择是否需要架构、数据或两者。
更新
要以编程方式创建数据库,您可以使用生成的 sql 并通过 ADO 执行它。网。
或者,如果您使用 CodeFirst,并且在测试类中为上下文设置 CreateDatabaseIfNotExists 初始值设定项,它可以自动执行此操作。您可以使用 实体框架对现有应用程序进行逆向工程,从数据库创建 CodeFirst 上下文Visual Studio 的强大工具。在这种情况下,您只需指定到不存在的数据库的连接字符串,CF 就会为您创建它。您还可以指定 DropCreateDatabaseAlways 在每次运行测试时刷新数据库(与每次在生成的 sql 脚本中添加相关数据库的 drop 后执行 sql 查询相同)
如果您使用codefirst,那么您不需要生成sql脚本。
如果您喜欢这些方法中的任何一种,我可以为您提供更多资源(如果您愿意)?
You can generate scripts by right clicking on database -> tasks -> generate scripts from sql management studio.

In wizard you can under advanced settings (depending on sql management tools version) choose whether you want schema, data, or both.
UPDATE
To create database programatically you can use generated sql and execute it via ADO.NET.
Or, if you use CodeFirst, it can do this automatically if you set CreateDatabaseIfNotExists initializer for context in your test class. You can create CodeFirst context from your database by reverse engineering existing application using Entity Framework Power Tools for Visual Studio. In that case you only need to specify connection string to non-existing database and CF will create it for you. You can also specify DropCreateDatabaseAlways to have your database refreshed each time when you run your test (same as executing sql query every time after you prepend drop for database in question to generated sql script)
If you use codefirst, then you do not need to generate sql scripts.
If you prefer any of these methods, I can provide you with more resources if you want?
您可以在 nhibernate 中以编程方式完成此操作。 Nhibernate 创建所有表
以及数据库中的关系,您仅在其中创建数据库实例(名称)
任何数据库服务器,如 Oracle、MySQL、MsSQL...
您可以查看此了解详细信息。
You can do it programatically in nhibernate. Nhibernate creates all the tables
and the relations in database you only create The Database instance (name) in
any Database server like Oracle, MySQL, MsSQL...
You can look this for details.
您可以使用SQL Server 管理对象类库中的对象。
例如, Database 类实现IScriptable。我会用它作为我的起点(但我自己还没有这样做)。此生成的脚本可能包含太多不相关的内容,在这种情况下,您可能必须枚举各个表并为它们生成脚本。
但如今,SMO 始终是“我如何针对 SQL Server 执行管理任务 X?”的答案。
You would use the objects in the SQL Server Management Objects Class library.
For instance, the Database class implements IScriptable. I'd use that as my starting point (but I haven't done this myself). It's possible that the script this generates might have too much that isn't relevant, in which case you might have to enumerate individual tables and generate scripts for them.
But SMO is always the answer (these days) to "how do I do management task X against SQL Server?".