Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#

Using Microsoft Enterprise Library Data Access Application Block – Part II

Rate me:
Please Sign up or sign in to vote.
3.07/5 (16 votes)
7 Aug 2009CPOL3 min read 88.6K   1.3K   28  
Using Microsoft Enterprise Library Data Access Application Block to Retrieve Data from Database.
using System;
using System.Data;
using Robin.Sample.Framework;


namespace Robin.Sample.Data
{
    public abstract class DataAccessComponent
    {
        protected string ConnectionString
        {
            get { return ConfigurationData.DBConnectionString; }
        }

        protected T GetDataValue<T>(IDataReader vobjDataReader, string vstrColumnName)
        {
            // NOTE: GetOrdinal() is used to automatically determine where the column
            //       is physically located in the database table. This allows the
            //       schema to be changed without affecting this piece of code.
            //       This of course sacrifices a little performance for maintainability.

            int intOrdinal = vobjDataReader.GetOrdinal(vstrColumnName);

            if (!vobjDataReader.IsDBNull(intOrdinal))
                return (T)vobjDataReader.GetValue(intOrdinal);
            else
                return default(T);
        }

        protected T GetDataValue<T>(IDataReader vobjDataReader, int vintColumnIndex)
        {
            if (!vobjDataReader.IsDBNull(vintColumnIndex))
                return (T)vobjDataReader.GetValue(vintColumnIndex);
            else
                return default(T);
        }
    }
}

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)



Comments and Discussions