私がReadループに到達すると、インデックスが範囲外になります。私は読者の秩序の価値については思っていますが、なぜ私はそれを得ているのか分かりません。インデックスが境界外の例外を返しているのはなぜですか?
Private Function Create(Reader As SqlDataReader) As IEnumerable(Of MyObject)
SetOrdinals(MyObjectReader)
Dim MyObjects = New List(Of MyObject)
While MyObjectReader.Read()
Dim Temp = New MyObject() With {
.FirstValue = MyObjectReader.GetValue(Of Integer)(MyObjectReader(FirstValue_Ord)),
.SecondValue = If(MyObjectReader.GetValue(Of String)(MyObjectReader(SecondValue_Ord)), String.Empty).Trim(),
.ThirdValue = If(MyObjectReader.GetValue(Of String)(MyObjectReader(ThirdValue_Ord)), String.Empty).Trim(),
MyObjects.Add(Temp)
End While
Return MyObjects
End Function
Private Sub SetOrdinals(MyObjectReader As SqlDataReader)
FirstValueOrd = MyObjectReader.GetOrdinal("FirstValue")
SecondValue_Ord = MyObjectReader.GetOrdinal("SecondValue")
ThirdValue_Ord = MyObjectReader.GetOrdinal("ThirdValue")
End Sub
End Class
Public Module Extensions
<Extension>
Function GetValue(Of T)(rdr As SqlDataReader, i As Integer) As T
If rdr.IsDBNull(i) Then
Return Nothing
End If
Return DirectCast(rdr.GetValue(i), T)
End Function
End Module
あなたは車輪を再発明しているようですね! 'SqlDataReader'には' GetString'や 'GetInteger'などのメソッドがあり、' GetValue'メソッドの処理を行います。 –
@ChrisDunawayこの遅れて申し訳ありません。 'GetString'などのメソッドではヌルチェックが必要です。そのため、私は' DataRow'の拡張メソッドと同様に[元の質問](http://stackoverflow.com/a/40683355)の 'GetValue'拡張を提案しました。それを処理するより良い方法はありますか? – Mark