Click here to Skip to main content
15,894,017 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.Collections;
using System.Data;
using System.Data.SqlClient;

namespace SqlProc
{
    /// <summary>
    /// Represents a single output column in the returned dataset from a stored procedure command.
    /// Also provides a static method to discover the output columns from a database.
    /// </summary>
    public class SqlOutputField
    {
        #region Static methods
        /// <summary>
        /// Returns an array of all output fields from this procedure.
        /// If the stored procedure does not return any data then an empty array is returned.
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="procedure"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static SqlOutputField[] GetOutputFields(string connectionString, string procedure, SqlParameter[] parameters)
        {
            SqlOutputField[] fields = new SqlOutputField[] {};
            try
            {
                fields = GetOutputFieldsFromDatabase(connectionString, procedure, parameters);
            }
            catch
            {
            }
            return fields;
        }

        /// <summary>
        /// Returns an array of all output fields from this procedure.
        /// If the stored procedure does not return any data then an exception is thrown.
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="procedure"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private static SqlOutputField[] GetOutputFieldsFromDatabase(string connectionString, string procedure, SqlParameter[] parameters)
        {
            ArrayList fields = new ArrayList();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, procedure, parameters)) 
            {
                for(int i = 0; i < reader.FieldCount; i++)
                {
                    string clrType = reader.GetFieldType(i).Name;
                    string sqlType = reader.GetDataTypeName(i);
                    string name = reader.GetName(i);
                    fields.Add(new SqlOutputField(name, i, sqlType, clrType, SqlTypeMapper.GetCSharpTypeName(clrType)));
                }
            }
            SqlOutputField[] farray = (SqlOutputField[])fields.ToArray(typeof(SqlOutputField));
            return farray;
        }
        #endregion

        #region Constructor
        public SqlOutputField(string field, int ordinal, string sqlType, string clrType, string csharpType)
        {
            Fieldname = field;
            Ordinal = ordinal;
            SqlTypeName = sqlType;
            CLRTypeName = clrType;
            CSharpTypeName = csharpType;
        }
        #endregion

        #region Properties
        public string Fieldname = string.Empty;
        public int Ordinal = 0;
        /// <summary>
        /// Returns the Sql type name as determined by the SqlReader.
        /// </summary>
        public string SqlTypeName = string.Empty;
        /// <summary>
        /// Returns the CLR type name as determined by the SqlReader.
        /// </summary>
        public string CLRTypeName = string.Empty;
        /// <summary>
        /// Returns the C# type name that corresponds to the CLR type of this field.
        /// </summary>
        public string CSharpTypeName = string.Empty;
        #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