I have a database query which will either return NULL
or a boolean (bit) value.
I wish to store this value in a variable of type Nullable
I use extension methods for this issue.
var isRestricted = dataRecord.GetNullableValue("IsRestricted");
There is code of GetNullableValue method:
public static Nullable GetNullableValue(
this IDataRecord record,
string name) where TValue : struct
{
return record.GetValue>(name);
}
And there is also a simple code for GetValue method:
private static TResult GetValue(
this IDataRecord record,
string name)
{
var result = record[name];
return !result.Equals(DBNull.Value) ? (TResult)result : default(TResult);
}