Generic methods to read value from DataReader

Reading a value from DataReader is one of the most often and repetitive task for programmers working on database project. Here are the generic methods to make this work easy:

Method static T GetData<T> gets value from the specified DataReader for the specified Column name with the option to return default value or throw exception in case of DBNull.

   1: private static T GetData<T>(IDataReader reader, string columnName, T valueInCaseOfDBNull, bool throwExpIfDBNull)

   2: {

   3: string errMsg = "Error from DataReaderHelper.GetData, Column Type : " + typeof(T).ToString() + " Additional Info : {0}.";

   4: try

   5: {

   6:     if (reader != null && !string.IsNullOrEmpty(columnName))

   7:     {

   8:         int columnIndex = reader.GetOrdinal(columnName);

   9:         T retValue;

  10:         if (!reader.IsDBNull(columnIndex))

  11:         {

  12:             retValue = (T)reader.GetValue(columnIndex);

  13:  

  14:         }

  15:         else

  16:         {

  17:             if (throwExpIfDBNull)

  18:             {

  19:                 throw new DataException(string.Format(errMsg,  "DBNull found while reading " + columnName));

  20:             }

  21:             else

  22:             {

  23:                 retValue = valueInCaseOfDBNull;

  24:             }

  25:         }

  26:         return retValue;

  27:     }

  28:     else

  29:     {

  30:         throw new DataException(string.Format(errMsg, "DataReader object is null or ColumnName is null or empty"));

  31:     }

  32: }

  33: catch (Exception ex)

  34: {

  35:     throw new DataException(string.Format(errMsg, "Error while reading column " + columnName), ex);

  36: }

  37:  

  38: }

And below are the three friendly public overloads to access above GetData method:

   1: /// <summary>

   2: /// Gets value from the specified DataReader for the specified Column name and

   3: /// throws exception if corresponding field name contains DBNull

   4: /// </summary>

   5: public static T GetData<T>(IDataReader dr, string columnName)

   6: {

   7:     return GetData<T>(dr, columnName, default(T), true);

   8: }

   9:  

  10: /// <summary>

  11: /// Gets value from the specified DataReader for the specified Column name with

  12: /// the option to throw exception or retun default value in case of DBNull

  13: /// </summary>

  14: public static T GetData<T>(IDataReader dr, string columnName, bool throwExpIfDBNull)

  15: {

  16:     return GetData<T>(dr, columnName, default(T), throwExpIfDBNull);

  17: }

  18:  

  19: /// <summary>

  20: /// Gets value from the specified DataReader for the specified Column name and

  21: /// retuns specified default value in case of DBNull

  22: /// </summary>

  23: public static T GetData<T>(IDataReader dr, string columnName, T valueInCaseOfDBNull)

  24: {

  25:     return (T)GetData(dr, columnName, valueInCaseOfDBNull, false);

  26: }

   2: {

   3: string errMsg = "Error from DataReaderHelper.GetData, Column Type : " + typeof(T).ToString() + " Additional Info : {0}.";

   4: try

   5: {

   6:     if (reader != null && !string.IsNullOrEmpty(columnName))

   7:     {

   8:         int columnIndex = reader.GetOrdinal(columnName);

   9:         T retValue;

  10:         if (!reader.IsDBNull(columnIndex))

  11:         {

  12:             retValue = (T)reader.GetValue(columnIndex);

  13:  

  14:         }

  15:         else

  16:         {

  17:             if (throwExpIfDBNull)

  18:             {

  19:                 throw new DataException(string.Format(errMsg,  "DBNull found while reading " + columnName));

  20:             }

  21:             else

  22:             {

  23:                 retValue = valueInCaseOfDBNull;

  24:             }

  25:         }

  26:         return retValue;

  27:     }

  28:     else

  29:     {

  30:         throw new DataException(string.Format(errMsg, "DataReader object is null or ColumnName is null or empty"));

  31:     }

  32: }

  33: catch (Exception ex)

  34: {

  35:     throw new DataException(string.Format(errMsg, "Error while reading column " + columnName), ex);

  36: }

  37:  

  38: }

And below are the three friendly public overloads to access above GetData method:

   1: /// <summary>

   2: /// Gets value from the specified DataReader for the specified Column name and

   3: /// throws exception if corresponding field name contains DBNull

   4: /// </summary>

   5: public static T GetData<T>(IDataReader dr, string columnName)

   6: {

   7:     return GetData<T>(dr, columnName, default(T), true);

   8: }

   9:  

  10: /// <summary>

  11: /// Gets value from the specified DataReader for the specified Column name with

  12: /// the option to throw exception or retun default value in case of DBNull

  13: /// </summary>

  14: public static T GetData<T>(IDataReader dr, string columnName, bool throwExpIfDBNull)

  15: {

  16:     return GetData<T>(dr, columnName, default(T), throwExpIfDBNull);

  17: }

  18:  

  19: /// <summary>

  20: /// Gets value from the specified DataReader for the specified Column name and

  21: /// retuns specified default value in case of DBNull

  22: /// </summary>

  23: public static T GetData<T>(IDataReader dr, string columnName, T valueInCaseOfDBNull)

  24: {

  25:     return (T)GetData(dr, columnName, valueInCaseOfDBNull, false);

  26: }

-->