简体   繁体   English

以编程方式调用 SQL 脚本向导

[英]Invoke SQL Script Wizard programmatically

Could someone please tell me if there is someway to invoke the SQL Script Wizard programmatically or through the command line?有人可以告诉我是否有办法以编程方式或通过命令行调用 SQL 脚本向导?

It's a great tool for deployment but I am sick of having to set the countless options each time I use it.这是一个很好的部署工具,但我厌倦了每次使用它时都必须设置无数选项。

The SSMS scripting wizard is just a shell over the Scripter SMO object capabilities. SSMS 脚本向导只是Scripter SMO object 功能之上的 shell。 From the scripting example on MSDN:从 MSDN 上的脚本示例:

using System;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Sdk.Sfc;


public class A {
   public static void Main() { 
      String dbName = "AdventureWorksLT2008R2"; // database name

      // Connect to the local, default instance of SQL Server. 
      Server srv = new Server();

      // Reference the database.  
      Database db = srv.Databases[dbName];

      // Define a Scripter object and set the required scripting options. 
      Scripter scrp = new Scripter(srv);
      scrp.Options.ScriptDrops = false;
      scrp.Options.WithDependencies = true;
      scrp.Options.Indexes = true;   // To include indexes
      scrp.Options.DriAllConstraints = true;   // to include referential constraints in the script

      // Iterate through the tables in database and script each one. Display the script.   
      foreach (Table tb in db.Tables) { 
         // check if the table is not a system table
         if (tb.IsSystemObject == false) {
            Console.WriteLine("-- Scripting for table " + tb.Name);

            // Generating script for table tb
            System.Collections.Specialized.StringCollection sc = scrp.Script(new Urn[]{tb.Urn});
            foreach (string st in sc) {
               Console.WriteLine(st);
            }
            Console.WriteLine("--");
         }
      } 
   }

You should be writing the scripts yourself as you create and change table structures, they should be in source control and associated with a particular release.您应该在创建和更改表结构时自己编写脚本,它们应该在源代码控制中并与特定版本相关联。 There is no excuse for treating database changes any different than any other code and database changes should never be made using the GUI.没有理由将数据库更改与任何其他代码进行任何不同的处理,并且永远不应该使用 GUI 进行数据库更改。

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

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