我正在尝试为我正在使用的本地数据库创建数据库脚本工具.
我已经能够为表,主键,索引和外键生成创建脚本,但我找不到任何方法来为表默认值生成创建脚本.
对于索引,它就像
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属性.是否有其他方法为表默认值生成脚本?
尝试使用带有DriAll选项集的Scripter对象:
Server server = new Server(@".\SQLEXPRESS"); Database db = server.Databases["AdventureWorks"]; Listlist = new List (); 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());