Extension Methods: IDataReader.GetValue<T>
A convenient solution to get value from IDataReader field.
/// <summary>
/// Gets the reader field value.
/// </summary>
/// <typeparam name="TField">The type of the field.</typeparam>
/// <param name="reader">The reader.</param>
/// <param name="fieldName">Name of the field.</param>
/// <returns></returns>
public static TField GetValue<TField>(this IDataReader reader, string fieldName)
{
//Guard
if (string.IsNullOrEmpty(fieldName)) throw new ArgumentException("fieldName");
return (TField)reader.GetValue(reader.GetOrdinal(fieldName));
}
Usage:
IDataReader reeader = _database.ExecuteReader(command);
if (reader.Read())
{
int productId = reader.GetValue<int>("ProductId");
}