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

C# 使用SMO获取表默认值的创建脚本,c#,sql-server,smo,C#,Sql Server,Smo,我正在尝试为我正在使用的本地数据库创建一个数据库脚本工具 我已经能够为表、主键、索引和外键生成创建脚本,但是我找不到任何方法为表默认值生成创建脚本 对于索引,它与 foreach (Index index in table.Indexes) { ScriptingOptions drop = new ScriptingOptions(); drop.ScriptDrops = true; drop.IncludeIfNotExists = true; forea

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

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

对于索引,它与

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属性。是否有其他方法为表默认值生成脚本?

虽然我没有使用SMO,但我查找了MSDN,下面是我的发现

表有一个Columns属性(column collection),它应该引用每一列
每列都有一个DefaultConstraint属性

这就是您要寻找的吗?

尝试使用对象和DriAll选项集:

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());
Server服务器=新服务器(@“\SQLEXPRESS”);
Database db=server.Databases[“AdventureWorks”];
列表=新列表();
DataTable DataTable=db.EnumObjects(DatabaseObjectTypes.Table);
foreach(dataTable.Rows中的DataRow行)
{
添加(新的Urn((字符串)行[“Urn”]);
}
Scripter Scripter=新的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.driindex=true;
scripter.Options.ClusteredIndex=true;
scripter.Options.NonClusteredIndexes=true;
scripter.Options.ToFileOnly=true;
scripter.Options.FileName=@“C:\tables.sql”;
scripter.Script(list.ToArray());

添加到Pavel的答案中。

我只需要得到一个不可修改表的脚本。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 + ";");
}

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

嗨,帕维尔,如果我只想为一个表生成脚本的话。例如:Production.Categories。我需要添加哪些脚本?Thanks@Goldfish请你另外问一个问题好吗?你可以在那里链接这个答案。