vb.net 如何检查 DataReader 值是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27765599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if DataReader value is not null?
提问by Thomas Carlton
I'm writing a VB.Net code that reads an Oracle Table through an SQL query.
我正在编写通过 SQL 查询读取 Oracle 表的 VB.Net 代码。
The SQL query may return some null columns. I'm trying to check if these columns are null or not but I'm receiving the error An exception of type 'System.InvalidCastException' occurred in Oracle.DataAccess.dll but was not handled in user code. The column contains some Null Data
SQL 查询可能会返回一些空列。我正在尝试检查这些列是否为空,但我收到错误Oracle.DataAccess.dll 中发生类型为“System.InvalidCastException”的异常,但未在用户代码中处理。该列包含一些空数据
Here is my code :
这是我的代码:
Dim Reader as OracleDataReader
'Execute the query here...
Reader.Read()
If IsNothing(Reader.GetDateTime(0)) Then 'Error here !!
'Do some staff
end if
Does anyone have an idea on how to check if a column is null please ?
有没有人知道如何检查列是否为空?
Thank you
谢谢
回答by ??ssa P?ngj?rdenlarp
Nothingmeans an object has not been initialized, DBNullmeans the data is not defined/missing. There are several ways to check:
Nothing表示对象尚未初始化,DBNull表示未定义/缺少数据。有几种检查方法:
' The VB Function
If IsDBNull(Reader.Item(0)) Then...
The GetDateTimemethod is problematic because you are asking it to convert a non value to DateTime. Item()returns Object which can be tested easily beforeconverting.
该GetDateTime方法有问题,因为您要求它将非值转换为 DateTime。 Item()返回可以在转换前轻松测试的对象。
' System Type
If System.DBNull.Value.Equals(...)
You can also the DbReader. This only works with the ordinal index, not a column name:
您也可以使用 DbReader。这仅适用于序数索引,不适用于列名:
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:
基于此,您可以将函数作为共享类成员组合在一起,也可以重新编写为扩展以测试 DBNull 并返回默认值:
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.
对于 DateTime 转换器,您必须决定返回什么。我更喜欢单独做这些。
回答by Bj?rn-Roger Kringsj?
You need to check if the field is null beforeyou 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
回答by Tony Dong
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))

