public override StringCollection Script(SmoScriptingContext context) { var scripter = new Microsoft.SqlServer.Management.Smo.Scripter(context.Server); Microsoft.SqlServer.Management.Smo.ScriptingOptions options = scripter.Options; options.IncludeDatabaseContext = context.ScriptDatabaseContext; StringCollection scriptingResult = scripter.Script(new[] { ScriptedObject }); var result = new StringCollection(); foreach (string scriptedBatch in scriptingResult) { result.Add(scriptedBatch); AddLineEnding(result); AddLineEnding(result); } return(result); }
private static void GenerateScript(SqlConnectionStringBuilder builderTenantLocal, string databaseTenantLiveName, FileInfo fileScript, Encoding encoding) { Console.WriteLine(); Console.WriteLine(); Console.Write($"GenerateScript."); var tableNames = new string[] { "__MigrationHistory", "Culture", "Department", "Folder", "Subscription", "Tenant", "TenantSubscription", "Role", "User", "UserFolder", "UserRole", "RoleUsers", }; using (var connection = new SqlConnection(builderTenantLocal.ConnectionString)) { var scriptDataInsert = ""; var scriptSchemaDrop = ""; var scriptSchemaCreate = ""; var serverConnection = new Microsoft.SqlServer.Management.Common.ServerConnection(connection); var server = new Microsoft.SqlServer.Management.Smo.Server(serverConnection); var database = server.Databases[builderTenantLocal.InitialCatalog]; var scripter = new Microsoft.SqlServer.Management.Smo.Scripter(server); var tables = new Microsoft.SqlServer.Management.Smo.UrnCollection(); File.WriteAllText(fileScript.FullName, $"USE [{databaseTenantLiveName}]{Environment.NewLine}{Environment.NewLine}", encoding); tables.Clear(); scripter.Options.ScriptDrops = true; scripter.Options.ScriptSchema = true; scripter.Options.ScriptData = false; scripter.Options.WithDependencies = true; scripter.Options.DriAllConstraints = true; scripter.Options.NoCommandTerminator = true; scripter.Options.IncludeDatabaseContext = false; scripter.Options.FileName = fileScript.FullName; scripter.Options.AppendToFile = true; scripter.Options.ToFileOnly = false; scripter.Options.IncludeHeaders = true; scripter.Options.IncludeIfNotExists = true; scripter.Options.PrimaryObject = true; scripter.Options.ExtendedProperties = true; scripter.Options.Encoding = encoding; foreach (Microsoft.SqlServer.Management.Smo.Table table in database.Tables) { tables.Add(table.Urn); } scriptSchemaDrop = string.Join(Environment.NewLine, scripter.EnumScript(tables)); tables.Clear(); scripter.Options.ScriptDrops = false; scripter.Options.ScriptSchema = true; scripter.Options.ScriptData = false; scripter.Options.WithDependencies = true; scripter.Options.DriAllConstraints = true; scripter.Options.NoCommandTerminator = true; scripter.Options.IncludeDatabaseContext = false; scripter.Options.FileName = fileScript.FullName; scripter.Options.AppendToFile = true; scripter.Options.ToFileOnly = false; scripter.Options.IncludeHeaders = true; scripter.Options.IncludeIfNotExists = true; scripter.Options.PrimaryObject = true; scripter.Options.ExtendedProperties = true; scripter.Options.Encoding = encoding; foreach (Microsoft.SqlServer.Management.Smo.Table table in database.Tables) { tables.Add(table.Urn); } scriptSchemaCreate = string.Join(Environment.NewLine, scripter.EnumScript(tables)); tables.Clear(); scripter.Options.ScriptDrops = false; scripter.Options.ScriptSchema = false; scripter.Options.ScriptData = true; scripter.Options.WithDependencies = true; scripter.Options.DriAllConstraints = true; scripter.Options.NoCommandTerminator = true; scripter.Options.IncludeDatabaseContext = false; scripter.Options.FileName = fileScript.FullName; scripter.Options.AppendToFile = true; scripter.Options.ToFileOnly = false; scripter.Options.IncludeHeaders = true; scripter.Options.IncludeIfNotExists = true; scripter.Options.PrimaryObject = true; scripter.Options.ExtendedProperties = true; scripter.Options.Encoding = encoding; foreach (var tableName in tableNames) { tables.Add(database.Tables[tableName].Urn); } scriptDataInsert = string.Join(Environment.NewLine, scripter.EnumScript(tables)); } }