以编程方式调用SQL脚本向导 [英] Invoke SQL Script Wizard programmatically

查看:171
本文介绍了以编程方式调用SQL脚本向导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我是否有程序或通过命令行调用SQL脚本向导?

推荐答案

SSMS脚本编制向导是一个用于部署的工具,但我不得不在每次使用它时设置无数的选项。只是 Scripter SMO对象功能。从MSDN上的脚本示例:

The SSMS scripting wizard is just a shell over the Scripter SMO object capabilities. From the scripting example on 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("--");
         }
      } 
   }

这篇关于以编程方式调用SQL脚本向导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆