public void TestSetup() { //start with default options string connectionString = ConfigurationManager.AppSettings["ConnectionString"]; string[] options = { connectionString }; RunOptions.Current.Logger = new TestLogger(); RunOptions.Current.Init(options); SqlConnection connection = new SqlConnection(connectionString); ServerConnection serverConnection = new ServerConnection(connection); Server server = new Server(serverConnection); Microsoft.SqlServer.Management.Smo.Database database = server.Databases[connection.Database]; if (database == null) { throw new ApplicationException("Database " + connection.Database + " not found."); } server.SetDefaultInitFields(typeof(View), true); database.PrefetchObjects(typeof(View)); for (int i = database.Views.Count - 1; i >= 0; i--) { View view = database.Views[i]; if (view.IsSystemObject) { continue; } view.Drop(); } server.SetDefaultInitFields(typeof(Table), true); database.PrefetchObjects(typeof(Table)); foreach (Table table in database.Tables) { if (table.IsSystemObject) { continue; } for (int i = table.ForeignKeys.Count - 1; i >= 0; i--) { table.ForeignKeys[i].Drop(); } } for (int i = database.Tables.Count - 1; i >= 0; i--) { Table table = database.Tables[i]; if (table.IsSystemObject) { continue; } table.Drop(); } server.SetDefaultInitFields(typeof(StoredProcedure), true); database.PrefetchObjects(typeof(StoredProcedure)); for (int i = database.StoredProcedures.Count - 1; i >= 0; i--) { StoredProcedure procedure = database.StoredProcedures[i]; if (procedure.IsSystemObject) { continue; } procedure.Drop(); } server.SetDefaultInitFields(typeof(UserDefinedFunction), true); database.PrefetchObjects(typeof(UserDefinedFunction)); for (int i = database.UserDefinedFunctions.Count - 1; i >= 0; i--) { UserDefinedFunction function = database.UserDefinedFunctions[i]; if (function.IsSystemObject) { continue; } function.Drop(); } server.SetDefaultInitFields(typeof(UserDefinedTableType), true); database.PrefetchObjects(typeof(UserDefinedTableType)); for (int i = database.UserDefinedTableTypes.Count - 1; i >= 0; i--) { UserDefinedTableType tableType = database.UserDefinedTableTypes[i]; if (!tableType.IsUserDefined) { continue; } tableType.Drop(); } server.SetDefaultInitFields(typeof(FullTextCatalog), true); for (int i = database.FullTextCatalogs.Count - 1; i >= 0; i--) { FullTextCatalog catalog = database.FullTextCatalogs[i]; catalog.Drop(); } }
private void ParseDatabase(SortedListCollectionBase items, Type itemType) { RunOptions.Current.Logger.Log("Getting " + itemType.Name + "s", OutputLevel.Reads); //Preload database schema try { if (itemType != typeof(Microsoft.SqlServer.Management.Smo.FullTextCatalog)) { database.PrefetchObjects(itemType, scriptOptions); } } catch (Exception e) { RunOptions.Current.Logger.Log(e.Message, OutputLevel.Errors); } //Find the objects List <SqlSmoObject> scripting = new List <SqlSmoObject>(); bool table = itemType == typeof(Microsoft.SqlServer.Management.Smo.Table); List <string> hackScripts = new List <string>(); foreach (ScriptNameObjectBase item in items) { if (item.Properties.Contains("IsSystemObject") && (bool)item.Properties["IsSystemObject"].Value) { continue; } if (!table && item.Properties.Contains("IsEncrypted") && (bool)item.Properties["IsEncrypted"].Value) { RunOptions.Current.Logger.Log("Cannot script encrypted object " + item.Name, OutputLevel.Reads); continue; } //Deal with the fact that we can't script out the text filegroup of tables //This isn't real SQL, but it'll work internally if (table) { string textFileGroup = (string)item.Properties["TextFileGroup"].Value; if (textFileGroup != "") { hackScripts.Add("ALTER TABLE " + item.ToString() + "SET TEXTIMAGE_ON " + textFileGroup); } } scripting.Add(item); } //Generate scripts Scripter scripter = new Scripter(server); scripter.Options = scriptOptions; System.Collections.Specialized.StringCollection scripted = scripter.Script(scripting.ToArray()); scripted.AddRange(hackScripts.ToArray()); //Parse everything up foreach (string script in scripted) { parsedDatabase.Parse(script); } }