使用 SMO 获取表默认值的创建脚本

发布于 2024-07-08 18:24:20 字数 672 浏览 6 评论 0原文

我正在尝试为我正在使用的本地数据库创建一个数据库脚本编写工具。

我已经能够为表、主键、索引和外键生成创建脚本,但我找不到任何方法为表默认值生成创建脚本。

对于索引,这很简单,

foreach (Index index in table.Indexes)
{
    ScriptingOptions drop = new ScriptingOptions();
    drop.ScriptDrops = true;
    drop.IncludeIfNotExists = true;

    foreach (string dropstring in index.Script(drop))
    {
        createScript.Append(dropstring);
    }

    ScriptingOptions create = new ScriptingOptions();
    create.IncludeIfNotExists = true;

    foreach (string createstring in index.Script(create))
    {
        createScript.Append(createstring);
    }
}

但是 Table 对象没有 Defaults 属性。 还有其他方法可以为表默认值生成脚本吗?

I'm trying to create a database scripter tool for a local database I'm using.

I've been able to generate create scripts for the tables, primary keys, indexes, and foreign keys, but I can't find any way to generate create scripts for the table defaults.

For indexes, it's as easy as

foreach (Index index in table.Indexes)
{
    ScriptingOptions drop = new ScriptingOptions();
    drop.ScriptDrops = true;
    drop.IncludeIfNotExists = true;

    foreach (string dropstring in index.Script(drop))
    {
        createScript.Append(dropstring);
    }

    ScriptingOptions create = new ScriptingOptions();
    create.IncludeIfNotExists = true;

    foreach (string createstring in index.Script(create))
    {
        createScript.Append(createstring);
    }
}

But the Table object doesn't have a Defaults property. Is there some other way to generate scripts for the table defaults?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴骨ら 2024-07-15 18:24:20

尝试使用 设置了 DriAll 选项的 Scripter 对象:

Server server = new Server(@".\SQLEXPRESS");
Database db = server.Databases["AdventureWorks"];
List<Urn> list = new List<Urn>();
DataTable dataTable = db.EnumObjects(DatabaseObjectTypes.Table);
foreach (DataRow row in dataTable.Rows)
{
   list.Add(new Urn((string)row["Urn"]));
}
Scripter scripter = new Scripter();
scripter.Server = server;
scripter.Options.IncludeHeaders = true;
scripter.Options.SchemaQualify = true;
scripter.Options.SchemaQualifyForeignKeysReferences = true;
scripter.Options.NoCollation = true;
scripter.Options.DriAllConstraints = true;
scripter.Options.DriAll = true;
scripter.Options.DriAllKeys = true;
scripter.Options.DriIndexes = true;
scripter.Options.ClusteredIndexes = true;
scripter.Options.NonClusteredIndexes = true;
scripter.Options.ToFileOnly = true;
scripter.Options.FileName = @"C:\tables.sql";
scripter.Script(list.ToArray());

Try using Scripter object with DriAll option set:

Server server = new Server(@".\SQLEXPRESS");
Database db = server.Databases["AdventureWorks"];
List<Urn> list = new List<Urn>();
DataTable dataTable = db.EnumObjects(DatabaseObjectTypes.Table);
foreach (DataRow row in dataTable.Rows)
{
   list.Add(new Urn((string)row["Urn"]));
}
Scripter scripter = new Scripter();
scripter.Server = server;
scripter.Options.IncludeHeaders = true;
scripter.Options.SchemaQualify = true;
scripter.Options.SchemaQualifyForeignKeysReferences = true;
scripter.Options.NoCollation = true;
scripter.Options.DriAllConstraints = true;
scripter.Options.DriAll = true;
scripter.Options.DriAllKeys = true;
scripter.Options.DriIndexes = true;
scripter.Options.ClusteredIndexes = true;
scripter.Options.NonClusteredIndexes = true;
scripter.Options.ToFileOnly = true;
scripter.Options.FileName = @"C:\tables.sql";
scripter.Script(list.ToArray());
二智少女猫性小仙女 2024-07-15 18:24:20

虽然我没有使用过 SMO,但我查阅了 MSDN,这是我发现的。

表有一个 Columns 属性(列集合),它应该引用每一列。
每个列都有一个 DefaultConstraint 属性。

这是你想要的?

While I haven't used SMO, I looked up MSDN and here is what I found.

Table has a Columns property (column collection), which should have reference to each column.
Each Column will have a DefaultConstraint property.

Is this what you are looking for?

画尸师 2024-07-15 18:24:20

除了帕维尔的答案。

我只需要获取单个表的脚本。 1.我想将表模式名称和表名称作为参数传递并生成脚本。 2. 将脚本分配给变量而不是写入文件。

单个表的代码:

/*get a particular table script only*/
Table myTable = db.Tables["TableName", "SchemaName"];
scripter.Script(new Urn[] { myTable.Urn});

将脚本写入变量:

StringCollection sc = scripter.Script(new Urn[] { myTable.Urn });
foreach (string script in sc)
{
    sb.AppendLine();
    sb.AppendLine("--create table");
    sb.Append(script + ";");
}

我希望它会对未来的读者有所帮助。

Addition to the Pavel's answer.

I needed to get the script for a invidual table only. 1. I wanted to pass the table schema name and table name as parameter and generate script. 2. assign the script to a variable rather than writing to file.

code for a individual table:

/*get a particular table script only*/
Table myTable = db.Tables["TableName", "SchemaName"];
scripter.Script(new Urn[] { myTable.Urn});

Write script to a variable:

StringCollection sc = scripter.Script(new Urn[] { myTable.Urn });
foreach (string script in sc)
{
    sb.AppendLine();
    sb.AppendLine("--create table");
    sb.Append(script + ";");
}

I hope it will help future readers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文