4

我有这个方法:

Private Function convertInteger(intInteger As Object) As Integer

    If IsDBNull(intInteger) Then
        convertInteger = 0
    Else
        convertInteger = cInt(intInteger)
    End If

End Function

但它返回此错误:

未为类型“整数”和类型“dbnull”定义运算符“=”

我试图将 DBnull 值转换为 0..

但问题是我试图转换的值并不总是 DBnull .. 那么我应该如何处理呢?

4

3 回答 3

3

尝试这个

Private Function convertInteger(intInteger As Object) As Integer

    If intInteger = DBNull.Value Then
        Return 0
    End If

    Return intInteger

End Function

正如 [Tim Schmelter] 所建议的,查看可空类型

于 2012-09-17T11:01:00.047 回答
1

尝试这个

Private Function convertInteger(ByVal intInteger As Object) as Integer
   If IsDBNull(intInteger) Then
       Return 0
   Else
       Return CInt(intInteger)
   End if
End Function
于 2015-03-30T10:27:43.683 回答
1
Private Sub readValue()
    Dim cSql As String
    Dim oCnn as OleDbConnection
    Dim oCmd as OleDbCommand
    Dim oDataReader as OleDbDataReader
    Dim valor as Integer

    oCnn.open()

    cSql = "SELECT FIELD_NAME FROM TABLE"
    oCmd = New OleDbCommand(cSql, oCnn)
    oReader = oCmd.executeReader

    if oReader.HasRows then
       oReader.read()
       valor = IIf(IsDBNull(oDataReader("FIELD_NAME")), 0, oDataReader("FIELD_NAME"))
    End If
End Sub
于 2019-10-08T19:44:19.133 回答