Skip to content

Instantly share code, notes, and snippets.

@mendhak
Created August 16, 2012 11:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mendhak/3369293 to your computer and use it in GitHub Desktop.
Save mendhak/3369293 to your computer and use it in GitHub Desktop.
Extension method to get value from a field in a SQLDataReader
public static class DataRecordExtensions
{
public static T GetValue<T>(this IDataRecord reader, string columnName)
{
object columnValue = reader[columnName];
T returnValue = default(T);
if (!(columnValue is DBNull))
{
returnValue = (T)Convert.ChangeType(columnValue, typeof(T));
}
return returnValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment