【问题标题】:Using SMO to script table as select使用 SMO 脚本表作为选择
【发布时间】:2010-05-28 14:26:37
【问题描述】:

SSMS 允许您右键单击表格和“将表格编写为”->“选择为”。我认为 SSMS 中的几乎所有内容都是通过 SMO 脚本引擎完成的,但我似乎找不到通过 SMO 执行此操作的方法,除非循环遍历列并自己生成脚本。

我的 Google-Fu 是不是很弱,还是人们只是不使用 SMO 来做这样的事情?我在任何地方都找不到任何示例脚本,但它似乎是一种常见的需求。

【问题讨论】:

    标签: sql sql-server-2008 scripting smo


    【解决方案1】:

    看起来像表的Script() 函数可以做到这一点:

    Server server = new Server(".");
    Database northwind = server.Databases["Northwind"];
    Table categories = northwind.Tables["Categories"];
    StringCollection script = categories.Script();
    string[] scriptArray = new string[script.Count];
    script.CopyTo(scriptArray, 0);
    

    现在scriptArray 将包含一个SQL 命令列表,字符串数组的第一个条目将是set ansi_nulls on。看到这个blog post

    【讨论】:

    • 谢谢,但这会生成一个创建脚本。我正在寻找一个选择脚本
    • 找不到生成选择脚本的方法。但这应该很容易实现吧?
    猜你喜欢
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多