4

使用しているローカル データベース用のデータベース スクリプト ツールを作成しようとしています。

テーブル、主キー、インデックス、および外部キーの作成スクリプトを生成できましたが、テーブルのデフォルトの作成スクリプトを生成する方法が見つかりません。

インデックスの場合、次のように簡単です

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 プロパティがありません。テーブルのデフォルトのスクリプトを生成する他の方法はありますか?

4

3 に答える 3

7

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());
于 2008-11-08T10:03:19.460 に答える
2

私は SMO を使用したことがありませんが、MSDN を調べたところ、ここにあることがわかりました。

Table には、各列への参照を持つ必要がある Columns プロパティ (列コレクション) があります。
各列には DefaultConstraint プロパティがあります。

これはあなたが探しているものですか?

于 2008-11-08T08:03:11.293 に答える