Click here to Skip to main content
15,884,986 members
Articles / Programming Languages / C#

C# 2.0 Nullable Data Readers

Rate me:
Please Sign up or sign in to vote.
4.90/5 (43 votes)
20 Jan 20067 min read 174K   1K   94  
An article on nullable data readers for .NET 2.0 nullable types.
#region Using Directives

using System;
using System.Data;

#endregion

namespace NullableReaders
{
    /// <summary>
    /// This class allows a DataRow to be read with a datareader-like interface.
    /// Since it implements INullableRader, it provides methods to read both
    /// non-null and nullable data fields.
    /// </summary>
    public class NullableDataRowReader : INullableReader
    {

        #region Private Fields

        private DataRow row;

        /// <summary>
        /// Delegate to be used for anonymous method delegate inference
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        private delegate T Conversion<T>(string name);

        #endregion


        #region Public Properties

        public DataRow Row
        {
            get
            {
                return row;
            }
            set
            {
                row = value;
            }
        }

        #endregion


        #region Constructors

        public NullableDataRowReader(DataRow dataRow)
        {
            this.row = dataRow;
        }

        public NullableDataRowReader()
        {
        }

        #endregion


        #region INullableReader Members

        public bool GetBoolean(string name)
        {
            return Convert.ToBoolean(row[name]);
        }

        public bool? GetNullableBoolean(string name)
        {
            return GetNullable<bool>(name, GetBoolean);
        }

        public byte GetByte(string name)
        {
            return Convert.ToByte(row[name]);
        }

        public byte? GetNullableByte(string name)
        {
            return GetNullable<byte>(name, GetByte);
        }

        public char GetChar(string name)
        {
            return Convert.ToChar(row[name]);
        }

        public char? GetNullableChar(string name)
        {
            return GetNullable<char>(name, GetChar);
        }

        public DateTime GetDateTime(string name)
        {
            return Convert.ToDateTime(row[name]);
        }

        public DateTime? GetNullableDateTime(string name)
        {
            return GetNullable<DateTime>(name, GetDateTime);
        }

        public decimal GetDecimal(string name)
        {
            return Convert.ToDecimal(row[name]);
        }

        public decimal? GetNullableDecimal(string name)
        {
            return GetNullable<decimal>(name, GetDecimal);
        }

        public double GetDouble(string name)
        {
            return Convert.ToDouble(row[name]);
        }

        public double? GetNullableDouble(string name)
        {
            return GetNullable<double>(name, GetDouble);
        }

        public float GetFloat(string name)
        {
            return Convert.ToSingle(row[name]);
        }

        public float? GetNullableFloat(string name)
        {
            return GetNullable<float>(name, GetFloat);
        }

        public Guid GetGuid(string name)
        {
            return (Guid)row[name];
        }

        public Guid? GetNullableGuid(string name)
        {
            return GetNullable<Guid>(name, GetGuid);
        }

        public short GetInt16(string name)
        {
            return Convert.ToInt16(row[name]);
        }

        public short? GetNullableInt16(string name)
        {
            return GetNullable<short>(name, GetInt16);
        }

        public int GetInt32(string name)
        {
            return Convert.ToInt32(row[name]);
        }

        public int? GetNullableInt32(string name)
        {
            return GetNullable<int>(name, GetInt32);
        }

        public long GetInt64(string name)
        {
            return Convert.ToInt64(row[name]);
        }

        public long? GetNullableInt64(string name)
        {
            return GetNullable<long>(name, GetInt64);
        }

        public string GetString(string name)
        {
            return row[name].ToString();
        }

        public string GetNullableString(string name)
        {
            if (row[name] == DBNull.Value)
            {
                return null;
            }
            else
            {
                return GetString(name);
            }
        }

        public object GetValue(string name)
        {
            return row[name];
        }

        public bool IsDBNull(string name)
        {
            return (row[name] == DBNull.Value);
        }

        #endregion


        #region Private Methods

        /// <summary>
        /// This generic method will be call by every interface method in the class.
        /// The generic method will offer significantly less code, with type-safety.
        /// Additionally, the methods can you delegate inference to pass the 
        /// appropriate delegate to be executed in this method.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name">Column name</param>
        /// <param name="convert">Delegate to invoke if the value is not DBNull</param>
        /// <returns></returns>
        private Nullable<T> GetNullable<T>(string name, Conversion<T> convert) where T : struct
        {
            Nullable<T> nullable;
            if (row[name] == DBNull.Value)
            {
                nullable = null;
            }
            else
            {
                nullable = convert(name);
            }
            return nullable;
        }

        #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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Steve Michelotti, MCSD, MCT is Principal Developer at e.magination in Baltimore - www.emagination.com.

Comments and Discussions