如何将 SQL 数据库的架构保存到文件中?
我正在用 C#.Net 编写一个连接到 SQL Server 数据库的软件应用程序。我的 C# 项目受 SVN 版本控制,但我也想将我的数据库架构包含在 SVN 存储库中。 我之前的问题的答案建议在版本控制中存储生成数据库的脚本。有没有办法从现有数据库自动生成这些脚本?
我对 SQL Server 很陌生,但我注意到在 Management Studio 中,可以通过右键单击表并单击“将表脚本编写为”来自动生成创建表的 SQL 命令。是否有适用于整个数据库的等效命令?
I'm writing a software application in C#.Net that connects to a SQL Server database. My C# project is under SVN version control, but I'd like to include my database schema in the SVN repository as well. An answer to a previous question of mine suggested storing the scripts to generate the database in version control. Is there a way to automatically generate these scripts from an existing database?
I'm very new to SQL Server, but I noticed in management studio that the SQL commands to create a table can be generated automatically by right clicking on the table and clicking "Script Table As". Is there an equivalent command that would work with the entire database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您没有提到哪个版本的 SQL Server,但是在 2008 年(可能更早),您可以右键单击数据库并选择“任务”->“任务”。生成脚本。向导将引导您完成这些步骤。
You didn't mention which version of SQL Server, but in 2008 (and possibly earlier), you can right-click the database and choose Tasks -> Generate Scripts. The wizard will walk you through the steps.
您可以使用 SQL-DMO 或SQL SMO 来编写脚本,或者像 APEXSQLScript 这样的产品(如果您有很多的依赖关系,您将需要使用像这样的成熟的第三方工具)。
You can use SQL-DMO or SQL SMO to script out, or a product like APEXSQLScript (if you have a lot of dependencies, you're going to want to use a mature 3rd party tool like this).
我们使用 Red-gate 比较,它允许(在专业版本中)与受 SVN 源代码控制的文件夹进行比较。提到的 Apex 工具可能具有类似的功能。
就我个人而言,我发现 SSMS 脚本生成器有点笨拙,并且更喜欢付费购买 Red Gate 的东西
We use Red-gate compare which allows comparison (in pro version) against a folder, which is under SVN source control. The Apex tool mentioned probably has a similar feature.
Personally, I find the SSMS script generator a tad clumsy and prefer to pay for Red Gate stuff
即使 SSMS 中没有 SQL 代理,您也可以执行此操作。
右键单击要创建脚本以导出架构的数据库>任务>生成脚本> 第三个屏幕上的“高级选项”>在“常规部分”的底部,您有“要编写脚本的数据类型”,其中包含“仅数据/架构”和“数据/仅架构”。
You can do that even without SQL Agent in SSMS.
Right click on DB you want create a script for export the schema for > Tasks > Generate Scripts > "Advanced Options" on 3th screen > In bottom of "General section" you have "Types of data to script" with Data Only/Schema and data/Schema Only.
我在生成内置脚本时遇到了麻烦。特别是,由于对象以错误的顺序编写脚本,尝试从生成的脚本重新创建数据库失败。 (所有对象都在那里,只是需要进行一些重新排序。)
有一个实用程序 ExecuteSQLScript 可以转储您指定的对象类型以编写脚本。它甚至可以为每个对象创建一个脚本,这对于 SCM 非常有用,因此您可以轻松跟踪单个对象的更改。
http://exportsqlscript.codeplex.com/
I've had trouble with the built-in script generation. In particular, trying to recreate the database from the generated script has failed due to objects being scripted in the wrong order. (All the objects were there, it just took some re-ordering.)
There is a utility ExecuteSQLScript that dumps the types of objects you specify to script. It can even create a script per object, which is very useful for SCM, so you can easily track changes to individual objects.
http://exportsqlscript.codeplex.com/