Click here to Skip to main content
15,881,204 members
Articles / Programming Languages / C#

SPGen - Stored Procedure Generator

Rate me:
Please Sign up or sign in to vote.
4.89/5 (49 votes)
12 Dec 2002MIT9 min read 392.7K   13.9K   162  
A simple app which generates INSERT and UPDATE SQL Stored Procedure code
using System;
using System.Collections;

namespace Bluegrass.Data
{
	/// <summary>
	/// Provides helper functions for SQLDMO
	/// </summary>
	public class SQLDMOHelper
	{
		public string ServerName;
		public string UserName;
		public string Password;
		public string Database;
		public string Table;

		/// <summary>
		/// SQLDMO.SQLServer Connection
		/// </summary>
		private SQLDMO.SQLServer Connection = new SQLDMO.SQLServerClass();

		/// <summary>
		/// Connects this.Connection
		/// </summary>
		public void Connect()
		{
			Connection.Connect(ServerName, UserName, Password);
		}

		/// <summary>
		/// DisConnects this.Connection
		/// </summary>
		public void DisConnect()
		{
			Connection.DisConnect();
		}

		/// <summary>
		/// An array of Registered SQL Servers
		/// </summary>
		/// <remarks>
		/// Thanks to Leppie @ CodeProject for the following
		/// </remarks>
		public Array RegisteredServers
		{
			get
			{
				ArrayList aServers = new ArrayList();
				SQLDMO.ApplicationClass acServers = new SQLDMO.ApplicationClass();

				for (int iServerGroupCount = 1; iServerGroupCount <= acServers.ServerGroups.Count; iServerGroupCount++)
					for (int iServerCount = 1; iServerCount <= acServers.ServerGroups.Item(iServerGroupCount).RegisteredServers.Count; iServerCount++)
						aServers.Add(acServers.ServerGroups.Item(iServerGroupCount).RegisteredServers.Item(iServerCount).Name);

				return aServers.ToArray();
			}
		}

			/// <summary>
			/// An array of Databases in a SQL Server
			/// </summary>
			public Array Databases
		{
			get
			{
				ArrayList aDatabases = new ArrayList();

				foreach(SQLDMO.Database dbCurrent in Connection.Databases)
					aDatabases.Add(dbCurrent.Name);

				return aDatabases.ToArray();
			}
		}

		/// <summary>
		/// Array of Tables in a Database
		/// </summary>
		public Array Tables
		{
			get
			{
				ArrayList aTables = new ArrayList();
				SQLDMO.Database dbCurrent = (SQLDMO.Database)Connection.Databases.Item(this.Database, Connection);

				foreach(SQLDMO.Table tblCurrent in dbCurrent.Tables)
					aTables.Add(tblCurrent.Name);
				
				return aTables.ToArray();
			}
		}

		/// <summary>
		/// A SQLDMO.Columns collection of Fields (Columns) in a Table
		/// </summary>
		public SQLDMO.Columns Fields
		{
			get
			{
				SQLDMO.Database dbCurrent = (SQLDMO.Database)Connection.Databases.Item(this.Database, Connection);
				SQLDMO.Table tblCurrent = (SQLDMO.Table)dbCurrent.Tables.Item(this.Table, Connection);

				return tblCurrent.Columns;
			}
		}
	}
}

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 MIT License


Written By
Web Developer Caliber AI
South Africa South Africa
My name is Paul Watson and I have been a professional web-developer since 1997. I live and work remotely from Cape Town, South Africa.

I have many years of experience with HTML, CSS, JavaScript, PostgreSQL, and Ruby on Rails. I am capable in Python and Machine Learning too.

Currently I am the CTO of CaliberAI. Formerly I worked with Kinzen (CTO & co-founder), Storyful (CTO, acquired by News Corp), FeedHenry (co-founder, acquired by Red Hat), and ChangeX.

Now that you know a bit about me why not say hello.

Comments and Discussions