Created
August 16, 2012 11:00
-
-
Save mendhak/3369293 to your computer and use it in GitHub Desktop.
Extension method to get value from a field in a SQLDataReader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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