为什么SMO脚本程序不会生成外键标头?

时间:2012-11-11 12:08:50

标签: c# sql-server-2008 tsql smo

我正在使用随SQL Server 2008一起发布的SMO库创建一个生成数据库模式的程序。

我已经获得了脚本编辑器输出代码,当它被配置为输出所有内容时,它几乎与SQL Server Management Studio输出相同,但有一个奇怪的例外:它不会为它生成的外键约束输出注释头SSMS确实如此。任何人都可以弄清楚这是为什么?这是我的代码:

private void btnExportScript_Click(object sender, EventArgs ea) {
    Server srv = setupConnection();

    // Reference the database
    if (!srv.Databases.Contains(cbChooseDb.SelectedItem.ToString())) {
        _utils.ShowError("Couldn't find DB '" + cbChooseDb.SelectedItem.ToString() + "'.");
        return;
    }
    Database db = srv.Databases[cbChooseDb.SelectedItem.ToString()];

    StringBuilder builder = new StringBuilder();
    try {
        Scripter scrp = new Scripter(srv);
        scrp.Options.AppendToFile = false;
        scrp.Options.ToFileOnly = false;
        scrp.Options.ScriptDrops = false;             // Don't script DROPs
        scrp.Options.Indexes = true;                  // Include indexes
        scrp.Options.DriAllConstraints = true;        // Include referential constraints in the script
        scrp.Options.Triggers = true;                 // Include triggers
        scrp.Options.FullTextIndexes = true;          // Include full text indexes
        scrp.Options.NonClusteredIndexes = true;      // Include non-clustered indexes
        scrp.Options.NoCollation = false;             // Include collation
        scrp.Options.Bindings = true;                 // Include bindings
        scrp.Options.SchemaQualify = true;            // Include schema qualification, eg. [dbo]
        scrp.Options.IncludeDatabaseContext = false;
        scrp.Options.AnsiPadding = true;
        scrp.Options.FullTextStopLists = true;
        scrp.Options.IncludeIfNotExists = false;
        scrp.Options.ScriptBatchTerminator = true;
        scrp.Options.ExtendedProperties = true;
        scrp.Options.ClusteredIndexes = true;
        scrp.Options.FullTextCatalogs = true;
        scrp.Options.SchemaQualifyForeignKeysReferences = true;
        scrp.Options.XmlIndexes = true;
        scrp.Options.IncludeHeaders = true;

        // Prefectching may speed things up
        scrp.PrefetchObjects = true;

        var urns = new List<Urn>();

        // Iterate through the tables in database and script each one.
        foreach (Table tb in db.Tables) {
            if (tb.IsSystemObject == false) {
                // Table is not a system object, so add it.
                urns.Add(tb.Urn);
            }
        }

        // Iterate through the views in database and script each one.  Display the script.
        foreach (Microsoft.SqlServer.Management.Smo.View view in db.Views) {
            if (view.IsSystemObject == false) {
                // View is not a system object, so add it.
                urns.Add(view.Urn);
            }
        }

        // Iterate through the stored procedures in database and script each one.  Display the script.
        foreach (StoredProcedure sp in db.StoredProcedures) {
            if (sp.IsSystemObject == false) {
                // Procedure is not a system object, so add it.
                urns.Add(sp.Urn);
            }
        }

        // Start by manually adding DB context
        builder.AppendLine("USE [" + db.Name + "]");
        builder.AppendLine("GO");

        System.Collections.Specialized.StringCollection sc = scrp.Script(urns.ToArray());
        foreach (string st in sc) {
            // It seems each string is a sensible batch, and putting GO after it makes it work in tools like SSMS.
            // Wrapping each string in an 'exec' statement would work better if using SqlCommand to run the script.
            builder.Append(st.Trim(new char[] { '\r', '\n' }) + "\r\nGO\r\n");
        }
    }
    catch (Exception ex) {
        showExceptionError("Couldn't generate script.", ex);
        return;
    }

    try {
        File.WriteAllText(txtExportToFile.Text, builder.ToString());
        _utils.ShowInfo("DB exported to script at: " + txtExportToFile.Text);
    }
    catch (Exception ex) {
        showExceptionError("Couldn't save script file.", ex);
        return;
    }
}

请注意,外键属于DRI约束类别,并且由于scrp.Options.DriAllConstraints = true;而编写脚本。

1 个答案:

答案 0 :(得分:0)

我在这里有一个解决方案:Can't get EnumScript() to generate constraints

由于某种原因,Scripter在给出Urns列表时不会发出DRI约束(外键等),但如果一次给出一个Urn,它将会发出。诀窍是按照祖先的顺序给出Urns:必须在约束引用之前定义表。为此,我使用了DependencyWalker

这是一个概要:

        var urns = new List<Urn>();
        Scripter schemaScripter = new Scripter(srv) { Options = schemaOptions };
        Scripter insertScripter = new Scripter(srv) { Options = insertOptions };
        var dw = new DependencyWalker(srv);

        foreach (Table t in db.Tables)
            if (t.IsSystemObject == false)
                urns.Add(t.Urn);
        DependencyTree dTree = dw.DiscoverDependencies(urns.ToArray(), true);
        DependencyCollection dColl = dw.WalkDependencies(dTree);

        foreach (var d in dColl)
        {
            foreach (var s in schemaScripter.Script(new Urn[] { d.Urn }))
                strings.Add(s);
            strings.Add("GO");
            if (scriptData)
            {
                int n = 0;
                foreach (var i in insertScripter.EnumScript(new Urn[] {d.Urn}))
                {
                    strings.Add(i);
                    if ((++n) % 100 == 0)
                        strings.Add("GO");
                }
            }
        }

注意:经常添加“GO”会使批处理大小变小,因此SSMS不会耗尽内存。