我有这种方法:
Private Function convertInteger(intInteger As Object) As Integer
If IsDBNull(intInteger) Then
convertInteger = 0
Else
convertInteger = cInt(intInteger)
End If
End Function
但它会返回此错误:
operator'='未定义类型'integer'并输入'dbnull'
我试图将DBnull值转换为0 ..
但问题是我尝试转换的值并不总是DBnull ..所以我该如何处理呢?
答案 0 :(得分:2)
试试这个
Private Function convertInteger(intInteger As Object) As Integer
If intInteger = DBNull.Value Then
Return 0
End If
Return intInteger
End Function
根据[Tim Schmelter]的建议,研究Nullable类型
答案 1 :(得分:1)
试试这个
Private Function convertInteger(ByVal intInteger As Object) as Integer
If IsDBNull(intInteger) Then
Return 0
Else
Return CInt(intInteger)
End if
End Function
答案 2 :(得分:0)
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