How to check if DataReader value is not null?

前端 未结 3 1627
忘掉有多难
忘掉有多难 2020-12-02 00:57

I\'m writing a VB.Net code that reads an Oracle Table through an SQL query.

The SQL query may return some null columns. I\'m trying to check if these columns are nul

相关标签:
3条回答
  • 2020-12-02 01:38

    You need to check if the field is null before you convert the value to date.

    If (Reader.IsDBNull(0)) Then
        'Null: Do not call GetDateTime
    End If
    

    If (Not Reader.IsDBNull(0)) Then
        'Not null: Retrieve the datetime.
        Dim dt As DateTime = Reader.GetDateTime(0)
    End If
    
    0 讨论(0)
  • 2020-12-02 01:39

    Using Generic function with extension, will make it easier.

    Imports System.Runtime.CompilerServices
    
    <Extension()>
    Public Module DataReaderExtensions
      Public Function GetValue(Of T)(ByVal drVar As Object) As T
        If drVar.Equals(DBNull.Value) Then
            ' Value is null, determine the return type for a default
            If GetType(T).Equals(GetType(String)) Then
                Return CType(CType("", Object), T)
            Else
                ' If it's anything else just return nothing
                Return CType(Nothing, T)
            End If
        Else
            ' Cast the value into the correct return type
            Return CType(drVar, T)
        End If
      End Function
    End Module
    

    And you can call it like

    dr.Item("abc").GetValue(string)
    dr.Item("def").GetValue(Nullable(of Date))
    
    0 讨论(0)
  • 2020-12-02 01:46

    Nothing means an object has not been initialized, DBNull means the data is not defined/missing. There are several ways to check:

    ' The VB Function
    If IsDBNull(Reader.Item(0)) Then...
    

    The GetDateTime method is problematic because you are asking it to convert a non value to DateTime. Item() returns Object which can be tested easily before converting.

     ' System Type
     If System.DBNull.Value.Equals(...)
    

    You can also the DbReader. This only works with the ordinal index, not a column name:

    If myReader.IsDbNull(index) Then 
    

    Based on that, you can put together functions either as Shared class members or reworked into Extensions to test for DBNull and return a default value:

    Public Class SafeConvert
        Public Shared Function ToInt32(Value As Object) As Integer
            If DBNull.Value.Equals(Value) Then
                Return 0
            Else
                Return Convert.ToInt32(Value)
            End If
        End Function
    
        Public Shared Function ToInt64(Value As Object) As Int64
            If DBNull.Value.Equals(Value) Then
                Return 0
            Else
                Return Convert.ToInt64(Value)
            End If
        End Function
    
        ' etc
    End Class
    

    Usage:

    myDate = SafeConvert.ToDateTime(Reader.Item(0))
    

    For a DateTime converter, you'd have to decide what to return. I prefer to do those individually.

    0 讨论(0)
提交回复
热议问题