Click here to Skip to main content
15,895,740 members
Articles / Database Development / SQL Server

SqlProcedure - Improve Database Performance, Eliminate Errors and Reduce Code

Rate me:
Please Sign up or sign in to vote.
4.66/5 (16 votes)
23 Nov 2007CPOL8 min read 75K   1.4K   66  
Provides a utility to generate a wrapper for stored procedures to improve performance and eliminate certain run-time errors
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

%USING%

namespace %NAMESPACE%
{
	/// <summary>
	/// Wrapper for the %PROCEDURE_NAME% stored procedure.
	/// </summary>
    public class %CLASS_NAME% : SqlProcedure
    {
%CONSTRUCTOR_START%
        public %CLASS_NAME%()
			: base("%PROCEDURE_NAME%")
		{
		}
%CONSTRUCTOR_END%

        public %CLASS_NAME%(%CONSTRUCTOR_PARAMS%)
			: base("%PROCEDURE_NAME%")
		{
%CONSTRUCTOR_BODY%
		}

        #region Input parameter values
%PARAM_PROPERTIES%

		/// <summary>
		/// Returns the collection of parameters required for this stored procecure. 
		/// The values are all set to DBNull.
		/// May return null if there are no parameters.
		/// </summary>
		/// <returns></returns>
		public override SqlParameter[] GetParameters()
		{
%GETPARAMETERS_BODY%
		}

		/// <summary>
		/// Sets the Value property of each of the given parameters.
		/// </summary>
		/// <param name="parameters"></param>
		public override void SetParameterValues(SqlParameter[] parameters)
		{
%SETPARAMETERVALUES_BODY%
		}

		/// <summary>
		/// Returns a full description of all parameters.
		/// </summary>
		/// <returns></returns>
		public override string ToParameterString()
		{
			return %TOPARAMETERSTRING%;
		}
        #endregion

        #region Column ordinal values
%ORDINAL_PROPERTIES%
        #endregion

        #region Output value class
		/// <summary>
		/// Represents all output values for a single returned record from the %PROCEDURE_NAME% stored procedure.
		/// </summary>
		public class %CLASS_NAME%_Values
		{
%VALUE_PROPERTIES%
		}
		#endregion

        #region Output values
		private %CLASS_NAME%_Values _value = new %CLASS_NAME%_Values();
		
		private ArrayList _valuesIList = new ArrayList();

		/// <summary>
		/// Gets the single output value.
		/// </summary>
		public %CLASS_NAME%_Values Value
		{
			get { return _value; }
		}

		/// <summary>
		/// Returns the array of all output values.
		/// </summary>
		/// <returns></returns>
		public %CLASS_NAME%_Values[] Values()
		{
			return (%CLASS_NAME%_Values[])_valuesIList.ToArray(typeof(%CLASS_NAME%_Values));
		}

		/// <summary>
		/// Assigns the properties that hold the output values based on the provided reader.
		/// </summary>
		/// <param name="reader"></param>
		public override void GetValues(SqlDataReader reader)
		{
			%CLASS_NAME%_Values v = new %CLASS_NAME%_Values();
%GETVALUES_BODY%
			_valuesIList.Add(v);
			_value = v;
		}
		#endregion

		#region Output fields
		public override SqlOutputField[] GetOutputFields()
		{
%GETOUTPUTFIELDS_BODY%
		}
        #endregion
	}
}

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
Architect
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