<legend id='vaaTZ'><style id='vaaTZ'><dir id='vaaTZ'><q id='vaaTZ'></q></dir></style></legend>
  • <tfoot id='vaaTZ'></tfoot>

    1. <small id='vaaTZ'></small><noframes id='vaaTZ'>

        <bdo id='vaaTZ'></bdo><ul id='vaaTZ'></ul>
      <i id='vaaTZ'><tr id='vaaTZ'><dt id='vaaTZ'><q id='vaaTZ'><span id='vaaTZ'><b id='vaaTZ'><form id='vaaTZ'><ins id='vaaTZ'></ins><ul id='vaaTZ'></ul><sub id='vaaTZ'></sub></form><legend id='vaaTZ'></legend><bdo id='vaaTZ'><pre id='vaaTZ'><center id='vaaTZ'></center></pre></bdo></b><th id='vaaTZ'></th></span></q></dt></tr></i><div id='vaaTZ'><tfoot id='vaaTZ'></tfoot><dl id='vaaTZ'><fieldset id='vaaTZ'></fieldset></dl></div>

      1. 如何检查 DataReader 值是否为空?

        时间:2023-09-17
      2. <legend id='59Qiw'><style id='59Qiw'><dir id='59Qiw'><q id='59Qiw'></q></dir></style></legend>

        <small id='59Qiw'></small><noframes id='59Qiw'>

        <tfoot id='59Qiw'></tfoot>
            <tbody id='59Qiw'></tbody>
            <bdo id='59Qiw'></bdo><ul id='59Qiw'></ul>
            <i id='59Qiw'><tr id='59Qiw'><dt id='59Qiw'><q id='59Qiw'><span id='59Qiw'><b id='59Qiw'><form id='59Qiw'><ins id='59Qiw'></ins><ul id='59Qiw'></ul><sub id='59Qiw'></sub></form><legend id='59Qiw'></legend><bdo id='59Qiw'><pre id='59Qiw'><center id='59Qiw'></center></pre></bdo></b><th id='59Qiw'></th></span></q></dt></tr></i><div id='59Qiw'><tfoot id='59Qiw'></tfoot><dl id='59Qiw'><fieldset id='59Qiw'></fieldset></dl></div>

                1. 本文介绍了如何检查 DataReader 值是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写通过 SQL 查询读取 Oracle 表的 VB.Net 代码.

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

                  SQL 查询可能会返回一些空列.我正在尝试检查这些列是否为空,但我收到错误 Oracle.DataAccess.dll 中发生类型为System.InvalidCastException"的异常,但未在用户代码中处理.该列包含一些空数据

                  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

                  这是我的代码:

                  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 ?

                  谢谢

                  推荐答案

                  Nothing 表示对象尚未初始化,DBNull 表示数据未定义/缺失.有几种方法可以检查:

                  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...
                  

                  GetDateTime 方法有问题,因为您要求它将非值转换为 DateTime.Item() 返回可以在转换之前轻松测试的对象.

                  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(...)
                  

                  您也可以使用 DbReader.这仅适用于序数索引,不适用于列名:

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

                  If myReader.IsDbNull(index) Then 
                  

                  基于此,您可以将函数放在一起作为共享类成员,也可以重新编写为扩展以测试 DBNull 并返回默认值:

                  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
                  

                  用法:

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

                  对于 DateTime 转换器,您必须决定返回什么.我更喜欢单独做这些.

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

                  这篇关于如何检查 DataReader 值是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在插入行时返回最后一个 ID (IDENTITY) VB.NET MySQL 下一篇:防止重复进入数据库

                  相关文章

                  1. <legend id='ey5Tb'><style id='ey5Tb'><dir id='ey5Tb'><q id='ey5Tb'></q></dir></style></legend>
                      <bdo id='ey5Tb'></bdo><ul id='ey5Tb'></ul>

                      <tfoot id='ey5Tb'></tfoot>

                      <small id='ey5Tb'></small><noframes id='ey5Tb'>

                    1. <i id='ey5Tb'><tr id='ey5Tb'><dt id='ey5Tb'><q id='ey5Tb'><span id='ey5Tb'><b id='ey5Tb'><form id='ey5Tb'><ins id='ey5Tb'></ins><ul id='ey5Tb'></ul><sub id='ey5Tb'></sub></form><legend id='ey5Tb'></legend><bdo id='ey5Tb'><pre id='ey5Tb'><center id='ey5Tb'></center></pre></bdo></b><th id='ey5Tb'></th></span></q></dt></tr></i><div id='ey5Tb'><tfoot id='ey5Tb'></tfoot><dl id='ey5Tb'><fieldset id='ey5Tb'></fieldset></dl></div>