Click here to Skip to main content
15,896,063 members
Articles / Database Development / SQL Server

SQL XML Documentation

Rate me:
Please Sign up or sign in to vote.
4.59/5 (12 votes)
29 Feb 2008CPOL5 min read 113.5K   1.4K   75  
How to create and compile SQL XML Documentation comments
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace JocysCom.Sql.ScriptsGenerator.Controls
{
	/// <summary>
	/// ProcedureHeaders user control.
	/// </summary>
	public partial class ProcedureHeaders : UserControl
	{
		/// <summary>
		/// ProcedureHeaders.
		/// </summary>
		public ProcedureHeaders()
		{
			InitializeComponent();
			dbaConnectionString = DbaConnectionStringTextBox.Text;
			if (dbaConnectionString.Length > 0)
			{
				CreateServer();
			}
		}

        System.Data.SqlClient.SqlConnectionStringBuilder scb;
		Microsoft.SqlServer.Management.Smo.Server server;
		Microsoft.SqlServer.Management.Smo.Database database;
		Microsoft.SqlServer.Management.Smo.StoredProcedure procedure;

		#region Database Connection Strings

		/// <summary>
		/// Hide connection string password.
		/// </summary>
		/// <param name="text"></param>
		/// <returns></returns>
		private string filterConnectionString(string text)
		{
			System.Text.RegularExpressions.Regex regex;
			regex = new System.Text.RegularExpressions.Regex("(Password|PWD)\\s*=([^;]*)([;]*)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
			return regex.Replace(text, "$1=<hidden>$3");
		}

		/// <summary>
		/// Database Administrative Connection String.
		/// </summary>
		private string dbaConnectionString
		{
			get { return m_dbaConnectionString; }
			set
			{
				this.m_dbaConnectionString = value;
				DbaConnectionStringTextBox.Text = filterConnectionString(value);
			}
		}
		private string m_dbaConnectionString;

		private void DbaConnectionButton_Click(object sender, EventArgs e)
		{
			Microsoft.Data.ConnectionUI.DataConnectionDialog dcd;
			dcd = new Microsoft.Data.ConnectionUI.DataConnectionDialog();
			//Adds all the standard supported databases
			//DataSource.AddStandardDataSources(dcd);
			//allows you to add datasources, if you want to specify which will be supported 
			dcd.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.SqlDataSource);
			dcd.SetSelectedDataProvider(Microsoft.Data.ConnectionUI.DataSource.SqlDataSource, Microsoft.Data.ConnectionUI.DataProvider.SqlDataProvider);
			dcd.ConnectionString = this.dbaConnectionString;
			Microsoft.Data.ConnectionUI.DataConnectionDialog.Show(dcd);
			if (dcd.DialogResult == DialogResult.OK)
			{
				this.dbaConnectionString = dcd.ConnectionString;
                CreateServer();
            }
		}

		#endregion

		#region Interface Control

		private void CreateServer()
		{
			scb = new System.Data.SqlClient.SqlConnectionStringBuilder(this.dbaConnectionString);
            server = new Microsoft.SqlServer.Management.Smo.Server(scb.DataSource);
            FillDatabases();
		}

        public bool FillDatabases()
        {
            DatabasesListBox.Items.Clear();
            InfoLabel.Text = "";
            if (!string.IsNullOrEmpty(scb.InitialCatalog))
            {
                DatabasesListBox.Items.Add(scb.InitialCatalog);
            }
            else
            {
                int length = 0;
                try
                {
                    length = server.Databases.Count;
                }
                catch (Exception ex)
                {
                    InfoLabel.Text = DateTime.Now.ToString("HH:mm:ss") + " Database: " + ex.InnerException.Message;
                }
                for (int i = 0; i < length; i++)
                {
                    DatabasesListBox.Items.Add(server.Databases[i].Name);
                }
            }
            return true;
        }

		private void DatabasesListBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			database = server.Databases[DatabasesListBox.SelectedIndex];
			FillProcedures();
		}

		private void FillProcedures()
		{
			ProceduresListBox.Items.Clear();
			int length = database.StoredProcedures.Count;
			for (int i = 0; i < length; i++)
			{
                if (!database.StoredProcedures[i].IsSystemObject && !database.StoredProcedures[i].IsEncrypted)
				{
					ProceduresListBox.Items.Add(database.StoredProcedures[i].Name);
				}
			}
		}

		private void ProceduresListBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			procedure = database.StoredProcedures[ProceduresListBox.Text];
			LoadCurrentProcedure();
		}

		private void RegenerateCommentButton_Click(object sender, EventArgs e)
		{
			XmlDocumentation.XmlComments xc = new XmlDocumentation.XmlComments(procedure, true);
			XmlCommentsTextBox.Text = xc.ToComments("--- ", true);
		}

		#endregion

		/// <summary>
		/// Load selected procedure.
		/// </summary>
		private void LoadCurrentProcedure()
		{
			HeaderTextBox.Text = procedure.TextHeader;
			BodyTextBox.Text = procedure.TextBody;
			InfoTextBox.Clear();
			InfoTextBox.AppendText("Create Date: " + procedure.CreateDate.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
			InfoTextBox.AppendText("database: " + server.Information.Version.Major.ToString() + "\r\n");
			if (server.Information.Version.Major > 8)
			{
				InfoTextBox.AppendText("Date Last Modified: " + procedure.DateLastModified.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
				InfoTextBox.AppendText("Method Name: " + procedure.MethodName + "\r\n");
			}
			InfoTextBox.AppendText("Extended Propereties: " + procedure.ExtendedProperties.Count.ToString() + "\r\n");
			InfoTextBox.AppendText("Name: " + procedure.Name + "\r\n");
			InfoTextBox.AppendText("Parameters Count: " + procedure.Parameters.Count.ToString() + "\r\n");
			for (int i = 0; i < procedure.Parameters.Count; i++)
			{

				InfoTextBox.AppendText("\t" + SqlObjectsToString.GetParameterWithType(procedure.Parameters[i]) + "\r\n");
			}
			InfoTextBox.AppendText("Properties Count: " + procedure.Properties.Count.ToString() + "\r\n");
			for (int i = 0; i < procedure.Properties.Count; i++)
			{
				InfoTextBox.AppendText("\t" + procedure.Properties[i].Name + " = " + procedure.Properties[i].Value.ToString() + "\r\n");
			}
			// Load XML Comments;
			string procedureText = procedure.Properties["Text"].Value.ToString();
			XmlDocumentation.XmlComments xc = new XmlDocumentation.XmlComments(procedure);
			XmlCommentsTextBox.Text = ((XmlDocument)xc.AllComments).DocumentElement.OuterXml;
		}

	}
}

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
Software Developer (Senior)
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