在对象初始值设定项中处理DBNull

时间:2015-01-23 01:59:55

标签: asp.net vb.net initialization asmx dbnull

在我的asp.net Web服务中,我有一个从数据库中获取数据的对象类,但是当数据库中的某些数据为空时,我会解决以下问题:

(1)如果我不处理数据库中的NULL值并使用如下代码:

<WebMethod> _
Public Function GetCustomerDetail(ByVal sqlQuery As String) As List(Of customerInfo)
    Dim detaillist = New List(Of customerInfo)()
    Dim detail As customerInfo

    Dim da = New SqlDataAdapter(sqlQuery, conn)
    Dim dt = New DataTable()
    da.Fill(dt)

    For Each dr As DataRow In dt.Rows
        detail = New customerInfo() With { _
         .CustomerID = dr("CUSTOMER_ID"), _
         .CustomerName = dr("CUSTOMER_NAME"), _
         .RegisterDate = dr("REGISTER_DATE"), _
         .Address = dr("ADDRESS") _
        }
        detaillist.Add(detail)
    Next
    Return detaillist

End Function

Public Class customerInfo
    Public CustomerID As String = String.Empty
    Public CustomerName As String = String.Empty
    Public RegisterDate As String = Date.Now.ToString("dd/MM/yyyy")
    Public Address As String = String.Empty
End Class

我收到了错误:

System.InvalidCastException:从类型'DBNull'到类型'String'的转换无效。

(2)如果我在数据库中处理NULL,如下所示:

<WebMethod> _
Public Function GetCustomerDetail(ByVal sqlQuery As String) As List(Of customerInfo)
    Dim detaillist = New List(Of customerInfo)()
    Dim detail As customerInfo

    Dim da = New SqlDataAdapter(sqlQuery, conn)
    Dim dt = New DataTable()
    da.Fill(dt)

    For Each dr As DataRow In dt.Rows
        detail = New customerInfo() With { _
         .CustomerID = dr("CUSTOMER_ID"), _
         .CustomerName = dr("CUSTOMER_NAME"), _
         .RegisterDate = dr("REGISTER_DATE"), _
         If dr("ADDRESS") = System.DBNull.Value Then
            .Address = ""
         Else
            .Address = dr("ADDRESS") _
         End if
        }
        detaillist.Add(detail)
    Next
    Return detaillist

End Function

Public Class customerInfo
    Public CustomerID As String = String.Empty
    Public CustomerName As String = String.Empty
    Public RegisterDate As String = Date.Now.ToString("dd/MM/yyyy")
    Public Address As String = String.Empty
End Class

我收到了错误:

编译器错误消息:BC30985:在对象初始值设定项中初始化的字段或属性的名称必须以“。”开头。

我想知道如何在对象初始值设定项中处理字符串和日期的DBNull值。

3 个答案:

答案 0 :(得分:2)

您可以使用Convert.ToString

<WebMethod> _
Public Function GetCustomerDetail(ByVal sqlQuery As String) As List(Of customerInfo)
    Dim detaillist = New List(Of customerInfo)()
    Dim detail As customerInfo

    Dim da = New SqlDataAdapter(sqlQuery, conn)
    Dim dt = New DataTable()
    da.Fill(dt)

    For Each dr As DataRow In dt.Rows
    Dim registerDate As Date
    If Date.TryParse(Convert.ToString(dr("REGISTER_DATE")), registerDate ) = False Then 
    'Do what you need to do if the cell is not a valid date time value
    End If
        detail = New customerInfo() With { _
         .CustomerID = Convert.ToString(dr("CUSTOMER_ID")), _
         .CustomerName = Convert.ToString(dr("CUSTOMER_NAME")), _
         .RegisterDate = registerDate.ToString("dd/MM/yyyy"), _
         .Address = Convert.ToString(dr("ADDRESS"))
        }
        detaillist.Add(detail)
    Next
    Return detaillist

End Function

根据OP的评论编辑。

答案 1 :(得分:1)

虽然其他方法可行,但我认为使用泛型支持的可重用扩展方法是理想的。

您可以将工作传递给扩展方法,并检查该值是否等于DBNull.Value的值

Public Module DataRowExtensions

    <System.Runtime.CompilerServices.Extension>
    Public Function GetValueOrDefault(Of TExpectedType)(dr As DataRow, propertyName As String) As TExpectedType
    If DBNull.Value.Equals(dr(propertyName)) Then
            Return Nothing
        End If

        Return DirectCast(dr(propertyName), TExpectedType)
    End Function
End Module

您可以看到此DotNetFiddle以查看各种数据类型的操作。

请注意,扩展方法Field<T>确实存在且类似,但它不会处理DBNull值。

答案 2 :(得分:0)

您不能在对象初始值设定项中使用if语句。 您必须实例化对象,然后在单独的行中设置属性。

detail = New customerInfo() 
'Then in separate lines, populate the properties individually

If dr("ADDRESS") = System.DBNull.Value Then
    detail.Address = ""
Else
    detail.Address = dr("ADDRESS")