private void Script(object[] scriptParams) { Server srv = (Server)scriptParams[0]; string[] lv = new String[2]; int srvVersion = srv.Version.Major; this.Invoke(new MethodInvoker(delegate { lv[0] = "Connecting to Database: " + scriptParams[1].ToString(); lv[1] = ""; lwProgress.Items.Add(new ListViewItem(lv, 0)); })); Database db = srv.Databases[scriptParams[1].ToString()]; Hashtable hsObject = (Hashtable)scriptParams[3]; this.Invoke(new MethodInvoker(delegate { ListViewItem lvi = lwProgress.Items[lwProgress.Items.Count - 1]; lvi.SubItems[1].Text = "OK"; })); string connectionString = srv.ConnectionContext.ConnectionString + "; Initial Catalog='" + scriptParams[1].ToString() + "'"; ObjectDB objectDB = new ObjectDB(connectionString); this.Invoke(new MethodInvoker(delegate { lv[0] = "Fetching Objects: "; lv[1] = ""; lwProgress.Items.Add(new ListViewItem(lv, 0)); })); ObjectHelper.ScriptingOptions so = (ObjectHelper.ScriptingOptions)scriptParams[2]; so.ServerMajorVersion = srv.VersionMajor; objectDB.ObjectFetched += new ObjectFetchedEventHandler(ObjectFetched); objectDB.FetchObjects(so); this.Invoke(new MethodInvoker(delegate { ListViewItem lvi = lwProgress.Items[lwProgress.Items.Count - 1]; lvi.SubItems[1].Text = "OK"; })); ScriptedObject scriptedObj; int objectCount = 0; objectCount = objectCount + objectDB.Aggregates.Count; objectCount = objectCount + objectDB.Assemblies.Count; objectCount = objectCount + objectDB.ApplicationRoles.Count; objectCount = objectCount + objectDB.BrokerPriorities.Count; objectCount = objectCount + objectDB.CLRTriggers.Count; objectCount = objectCount + objectDB.CLRUserDefinedFunctions.Count; objectCount = objectCount + objectDB.Contracts.Count; objectCount = objectCount + objectDB.DatabaseRoles.Count; objectCount = objectCount + objectDB.DDLTriggers.Count; objectCount = objectCount + objectDB.Defaults.Count; objectCount = objectCount + objectDB.DMLTriggers.Count; objectCount = objectCount + objectDB.FullTextCatalogs.Count; objectCount = objectCount + objectDB.FullTextStopLists.Count; objectCount = objectCount + objectDB.Indexes.Count; objectCount = objectCount + objectDB.MessageTypes.Count; objectCount = objectCount + objectDB.PartitionFunctions.Count; objectCount = objectCount + objectDB.PartitionSchemes.Count; objectCount = objectCount + objectDB.RemoteServiceBindings.Count; objectCount = objectCount + objectDB.Routes.Count; objectCount = objectCount + objectDB.Rules.Count; objectCount = objectCount + objectDB.Schemas.Count; objectCount = objectCount + objectDB.ServiceQueues.Count; objectCount = objectCount + objectDB.Services.Count; objectCount = objectCount + objectDB.SQLUserDefinedFunctions.Count; objectCount = objectCount + objectDB.StoredProcedures.Count; objectCount = objectCount + objectDB.Synonyms.Count; objectCount = objectCount + objectDB.Tables.Count; objectCount = objectCount + objectDB.UserDefinedDataTypes.Count; objectCount = objectCount + objectDB.UserDefinedTableTypes.Count; objectCount = objectCount + objectDB.UserDefinedTypes.Count; objectCount = objectCount + objectDB.Users.Count; objectCount = objectCount + objectDB.Views.Count; objectCount = objectCount + objectDB.XMLSchemaCollections.Count; foreach (Aggregate obj in objectDB.Aggregates) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "Aggregate"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (Assembly obj in objectDB.Assemblies) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Type = "Assembly"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.BrokerPriority obj in objectDB.BrokerPriorities) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Type = "BrokerPriority"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (Principal obj in objectDB.ApplicationRoles) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "ApplicationRole"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (CLRTrigger obj in objectDB.CLRTriggers) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(so); scriptedObj.Schema = obj.Schema; if (obj.IsDatabaseTrigger) { scriptedObj.Type = "CLRDDLTrigger"; } else { scriptedObj.Type = "CLRDMLTrigger"; } hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (CLRUserDefinedFunction obj in objectDB.CLRUserDefinedFunctions) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(so); scriptedObj.Schema = obj.Schema; if (obj.IsTableValued) { scriptedObj.Type = "CLRUserDefinedTableFunction"; } else { scriptedObj.Type = "CLRUserDefinedFunction"; } hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (Contract obj in objectDB.Contracts) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Type = "SERVICECONTRACT"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (Principal obj in objectDB.DatabaseRoles) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "DatabaseRole"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.Trigger obj in objectDB.DDLTriggers) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(so); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "SQLDDLTrigger"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.Default obj in objectDB.Defaults) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Type = "Default"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.Trigger obj in objectDB.DMLTriggers) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(so); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "SQLDMLTrigger"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.FullTextCatalog obj in objectDB.FullTextCatalogs) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(so); scriptedObj.Type = "FullTextCatalog"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.FullTextStopList obj in objectDB.FullTextStopLists) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Type = "FullTextStopList"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.Index obj in objectDB.Indexes) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(so); scriptedObj.Type = "Index"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.MessageType obj in objectDB.MessageTypes) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Type = "MessageType"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.PartitionFunction obj in objectDB.PartitionFunctions) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Type = "PartitionFunction"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.PartitionScheme obj in objectDB.PartitionSchemes) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Type = "PartitionScheme"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.RemoteServiceBinding obj in objectDB.RemoteServiceBindings) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Type = "REMOTESERVICEBINDING"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.Route obj in objectDB.Routes) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Type = "SERVICEROUTE"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.Rule obj in objectDB.Rules) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "Rule"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.Schema obj in objectDB.Schemas) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Type = "Schema"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.ServiceQueue obj in objectDB.ServiceQueues) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "SERVICEQUEUE"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.Service obj in objectDB.Services) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Type = "Service"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.SQLUserDefinedFunction obj in objectDB.SQLUserDefinedFunctions) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(so); scriptedObj.Schema = obj.Schema; if (obj.IsTableValued) { scriptedObj.Type = "SQLUserDefinedTableFunction"; } else { scriptedObj.Type = "SQLUserDefinedFunction"; } //scriptedObj.Type = "SQLUserDefinedFunction"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.StoredProcedure obj in objectDB.StoredProcedures) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(so); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "StoredProcedure"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.Synonym obj in objectDB.Synonyms) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "Synonym"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.Table obj in objectDB.Tables) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(so); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "Table"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.UserDefinedDataType obj in objectDB.UserDefinedDataTypes) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "UserDefinedDataType"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.UserDefinedTableType obj in objectDB.UserDefinedTableTypes) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(so); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "UserDefinedTableType"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.UserDefinedType obj in objectDB.UserDefinedTypes) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "UserDefinedType"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.Principal obj in objectDB.Users) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "User"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.View obj in objectDB.Views) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(so); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "View"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } foreach (ObjectHelper.DBObjectType.XMLSchemaCollection obj in objectDB.XMLSchemaCollections) { scriptedObj = new ScriptedObject(); scriptedObj.Name = obj.Name; scriptedObj.ObjectDefinition = obj.Script(); scriptedObj.Schema = obj.Schema; scriptedObj.Type = "XMLSCHEMACOLLECTION"; hsObject.Add(Guid.NewGuid().ToString(), scriptedObj); } }
private void Script(object[] scriptParams) { Server server = (Server)scriptParams[0]; string dbName = scriptParams[1].ToString(); so.ServerMajorVersion = server.VersionMajor; string connectionString = server.ConnectionContext.ConnectionString + "; Initial Catalog='" + dbName + "'"; ObjectDB objectDB = new ObjectDB(connectionString); objectDB.ObjectFetched += new ObjectFetchedEventHandler(ObjectFetched); objectDB.FetchObjects(so); string text = System.IO.File.ReadAllText(@"Template\toc.html"); StringBuilder sbToc = new StringBuilder(); sbToc.AppendLine("<ol class=\"tree\">"); if (so.Tables) { List <BaseDBObject> lst = objectDB.Tables.ConvertAll(obj => (BaseDBObject)obj); sbToc.AppendLine(GenerateTocList(lst, "Tables")); } if (so.Views) { List <BaseDBObject> lst = objectDB.Views.ConvertAll(obj => (BaseDBObject)obj); sbToc.AppendLine(GenerateTocList(lst, "Views")); } if (so.CLRTriggers || so.DDLTriggers) { List <BaseDBObject> lst = objectDB.CLRTriggers.ConvertAll(obj => (BaseDBObject)obj); foreach (ObjectHelper.DBObjectType.Trigger trigger in objectDB.DDLTriggers.ConvertAll(obj => (BaseDBObject)obj)) { lst.Add(trigger); } sbToc.AppendLine(GenerateTocList(lst, "Triggers")); } if (so.StoredProcedures) { List <BaseDBObject> lst = objectDB.StoredProcedures.ConvertAll(obj => (BaseDBObject)obj); sbToc.AppendLine(GenerateTocList(lst, "Stored Procedures")); } if (so.CLRUserDefinedFunctions || so.SQLUserDefinedFunctions) { List <BaseDBObject> lst = objectDB.CLRUserDefinedFunctions.ConvertAll(obj => (BaseDBObject)obj); foreach (ObjectHelper.DBObjectType.SQLUserDefinedFunction function in objectDB.SQLUserDefinedFunctions.ConvertAll(obj => (BaseDBObject)obj)) { lst.Add(function); } sbToc.AppendLine(GenerateTocList(lst, "Functions")); } if (so.DMLTriggers) { List <BaseDBObject> lst = objectDB.DMLTriggers.ConvertAll(obj => (BaseDBObject)obj); sbToc.AppendLine(GenerateTocList(lst, "DML Triggers")); } string bodyPos = text.Substring(0, text.IndexOf("<body")); int startBodyPos = text.IndexOf(">", text.IndexOf("<body")); int endBodyPos = text.IndexOf("</body>"); StringBuilder sbToc2 = new StringBuilder(); sbToc2.AppendLine(text.Substring(0, startBodyPos + 1)); sbToc.AppendLine("</ol>"); sbToc2.AppendLine(sbToc.ToString()); sbToc2.AppendLine(text.Substring(endBodyPos)); System.IO.TextWriter tw = new System.IO.StreamWriter(System.IO.Path.Combine(txtOutputForder.Text, "Documentation.html")); System.IO.File.Copy(@"Template\_styles.css", System.IO.Path.Combine(txtOutputForder.Text, "_styles.css"), true); System.IO.File.Copy(@"Template\document.png", System.IO.Path.Combine(txtOutputForder.Text, "document.png"), true); System.IO.File.Copy(@"Template\folder-horizontal.png", System.IO.Path.Combine(txtOutputForder.Text, "folder-horizontal.png"), true); System.IO.File.Copy(@"Template\toggle-small-expand.png", System.IO.Path.Combine(txtOutputForder.Text, "toggle-small-expand.png"), true); System.IO.File.Copy(@"Template\toggle-small.png", System.IO.Path.Combine(txtOutputForder.Text, "toggle-small.png"), true); // write a line of text to the file tw.WriteLine(sbToc2.ToString()); // close the stream tw.Close(); }