Click here to Skip to main content
15,893,668 members
Articles / Database Development / SQL Server

Using SQL Server Metadata and Statistics to Build a Table Explorer Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
1 Oct 2012CPOL8 min read 22.4K   488   7  
Develops an analogy between database table and file directory information and argues that it could be very useful if it were possible to have a similar application to Windows Explorer for database tables. Demonstrates how to obtain the information from Sql Server system tables and dynamic management
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

/*
	Class to represent the central configuration document
	Exposes the Xml docs, defines the tags to be used and 
	creates nodes etc.
	
	Use singleto pattern and have a static instance object 
	
*/

namespace DbView
{

	class ConfigDoc : XmlDocument
	{
		public const String CFGELM_DOC = "DBVIEW_CONFIG";

		public const String CFGELM_CONNECTIONS = "Connections";
		public const String CFGELM_CONNECTION = "Connection";
		public const String CFGELM_FAVOURITES = "Favourites";
		public const String CFGELM_FAVOURITE = "Favourite";
		public const String CFGELM_VIEWLINKS = "Links";
		public const String CFGELM_VIEWLINK = "Link";
		public const String CFGELM_VIEWDEFS = "Views";
		public const String CFGELM_VIEWDEF = "View";
	
		// view defs
		public const String CFGVIEWDEF_VIEWCOL = "ViewCol";
		public const String CFGVIEWDEF_SORTCOL = "SortCol";
		public const String CFGVIEWDEF_FILTERCOL = "FilterCol";
		public const String CFGVIEWDEF_NUMFILTERCOL = "NumFilterCol";
		public const String CFGVIEWDEF_EXTRAINFO = "Extra";
		public const String CFGVIEWDEF_FILTER = "Filter";
		public const String CFGVIEWDEF_JOINCLAUSE = "Join";
		
	
		public const String CFGATT_CONN = "conn";
		public const String CFGATT_NAME = "name";
		public const String CFGATT_DBOBJNAME = "name";
		public const String CFGATT_DBOBJTTYPE = "objtype";
		public const String CFGATT_TABLE = "table";
		public const String CFGATT_SERVER = "srvr";
		public const String CFGATT_DATABASE = "db";
		public const String CFGATT_USER = "uid";
		public const String CFGATT_PASSWORD = "pwd";
	
		public static ConfigDoc Instance
		{
			get 
			{
				if (theConfig == null)
					theConfig = new ConfigDoc();
				return theConfig;
			}
		}
	
		// careful with name Load() is used by the base class
		public void LoadConfig(String fileName)
		{
			try 
			{
				this.Load(fileName);
			}
			catch (System.IO.FileNotFoundException exc)
			{
				// Add root element
				XmlNode node = this.CreateElement(CFGELM_DOC);
				this.AppendChild(node);
			}
		}
	
		// careful with name Save() is used by the base class
		public void SaveConfig(String fileName)
		{
			this.Save(fileName);
		}
		
		// note send in the group name: appends a new element with the correct name
		public XmlNode CreateNewRecordInGroup(String GroupElementName)
		{
			String newElementName = (GroupElementName == CFGELM_FAVOURITES) ? CFGELM_FAVOURITE :
									(GroupElementName == CFGELM_VIEWLINKS) ? CFGELM_VIEWLINK :
									(GroupElementName == CFGELM_VIEWDEFS) ? CFGELM_VIEWDEF :
									(GroupElementName == CFGELM_CONNECTIONS) ? CFGELM_CONNECTION : "";
			if (newElementName.Length == 0)
				throw new Exception(String.Format("{0} not a config group element name",GroupElementName)); 

			XmlNode node = this.CreateElement(newElementName);
			XmlNode groupNode = getGroupNode(GroupElementName);
			groupNode.AppendChild(node);
			return node;
		}

		public XmlNode CreateNewChild(XmlNode parent, String ChildElementName)
		{
			XmlNode node = this.CreateElement(ChildElementName);
			parent.AppendChild(node);
			return node;
		}

		public void RemoveRecord(XmlNode node)
		{
			XmlNode parent = node.ParentNode;
			if (parent != null)
				parent.RemoveChild(node);
		}

		public XmlNode Connections
		{
			get { return getGroupNode(CFGELM_CONNECTIONS); }
		}

		public XmlNode Views
		{
			get { return getGroupNode(CFGELM_VIEWDEFS); }
		}

		public XmlNode Viewlinks
		{
			get { return getGroupNode(CFGELM_VIEWLINKS); }
		}

		public XmlNode Favourites
		{
			get { return getGroupNode(CFGELM_FAVOURITES); }
		}

		XmlNode getGroupNode(String GroupElementName)
		{
			String xpath = String.Format("/{0}/{1}", CFGELM_DOC, GroupElementName);
			XmlNode node = this.SelectSingleNode(xpath);
			if (node == null)
			{
				node = this.CreateElement(GroupElementName);
				this.DocumentElement.AppendChild(node);
			}
			return node;
		}
		
		public void AddAttribute(XmlNode node, String attName, String attVal)
		{
			XmlAttribute att = this.CreateAttribute(attName);
			node.Attributes.Append(att);
			att.Value = attVal;
		}
	
		public String AttributeValue(XmlNode node, String attName, String defaultVal)
		{
			XmlAttribute att = node.Attributes[attName];
			if (att == null) return defaultVal;
			return att.Value;
		}
		
		public String AttributeValue(XmlNode node, String attName)
		{
			return AttributeValue(node, attName, "");
		}
	
		public String ElementText(XmlNode node, String elementName)
		{
			XmlNode tgtNode = node.SelectSingleNode(elementName);
			if (tgtNode == null) return "";
			return tgtNode.InnerText;
		}

		public List<String> Elements(XmlNode node, String elementName)
		{	
			List<String> data = new List<string>();
			XmlNodeList Nodes = node.SelectNodes(elementName);
			foreach (XmlNode n in Nodes)
				data.Add(n.InnerText);
			return data;
		}
	
		static ConfigDoc theConfig = null;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions