Inspired by a recent thread regarding nullable value types and data readers, I decided to create some extension methods that would make working with nullable data easier. There are two types of methods in the attached file:
1. It has always annoyed me that the Item property of a data reader allows you to specify the column by ordinal or name, while the type-specific methods for retrieving data only support ordinals. For instance, you can do this:where the equivalent type-specific method doesn't work the same:vb.net Code:
Dim b1 As Boolean = myDataReader(0) Dim b2 As Boolean = myDataReader("Flag")To remedy this, for each type-specific method of a data reader that accepts an ordinal, I've created a corresponding method that accepts a name, e.g.vb.net Code:
Dim b1 As Boolean = myDataReader.GetBoolean(0) 'This works Dim b2 As Boolean = myDataReader.GetBoolean("Flag") 'This doesn't compileUsing this method, this code now compiles and runs as expected:vb.net Code:
<Extension()> Public Function GetBoolean(ByVal source As DbDataReader, ByVal name As String) As Boolean Return source.GetBoolean(source.GetOrdinal(name)) End FunctionNote that these methods have nothing to do with nullable data. They just make working with data readers a little easier as I think that it's more natural to use column names than ordinals.vb.net Code:
Dim b2 As Boolean = myDataReader.GetBoolean("Flag")
2. The next type of method does deal with nullable data; specifically, reading nullable data from a data reader. Using GetBoolean as an example again, you can do this:and it will work fine, as long as column 0 contains a value. If it doesn't than that code will throw and exception. Previously, if you wanted to get a nullable Boolean from a data reader it would look like this:vb.net Code:
Dim b1 As Boolean = myDataReader.GetBoolean(0)or, more succinctly:vb.net Code:
Dim b1 As Boolean? If myDataReader.IsDBNull(0) Then b1 = Nothing Else b1 = myDataReader.GetBoolean(0) End IfNote that the cast is required in that last code snippet because the If operator requires that both possible results are, or can be converted to, the same type. I have encapsulated that last code snippet into my extension methods:vb.net Code:
Dim b1 As Boolean? = If(myDataReader.IsDBNull(0), DirectCast(Nothing, Boolean?), myDataReader.GetBoolean(0))so now you can just do this:vb.net Code:
<Extension()> Public Function GetNullableBoolean(ByVal source As DbDataReader, ByVal ordinal As Integer) As Boolean? Return If(source.IsDBNull(ordinal), DirectCast(Nothing, Boolean?), source.GetBoolean(ordinal)) End FunctionI've also created a corresponding overload that takes a name rather than an ordinal:vb.net Code:
Dim b1 As Boolean? = myDataReader.GetNullableBoolean(0)so you can also do this:vb.net Code:
<Extension()> Public Function GetNullableBoolean(ByVal source As DbDataReader, ByVal name As String) As Boolean? Dim ordinal As Integer = source.GetOrdinal(name) Return If(source.IsDBNull(ordinal), DirectCast(Nothing, Boolean?), source.GetBoolean(ordinal)) End Functionvb.net Code:
Dim b1 As Boolean? = myDataReader.GetNullableBoolean("Flag")


Reply With Quote
