DataTableReader.IsDBNull(Int32) DataTableReader.IsDBNull(Int32) DataTableReader.IsDBNull(Int32) DataTableReader.IsDBNull(Int32) Method

定义

获取一个值,该值指示列中是否包含不存在的或缺少的值。Gets a value that indicates whether the column contains non-existent or missing values.

public:
 override bool IsDBNull(int ordinal);
public override bool IsDBNull (int ordinal);
override this.IsDBNull : int -> bool
Public Overrides Function IsDBNull (ordinal As Integer) As Boolean

参数

ordinal
Int32 Int32 Int32 Int32

从零开始的列序号The zero-based column ordinal

返回

如果指定的列值与 DBNull 等效,则为 true;否则为 falsetrue if the specified column value is equivalent to DBNull; otherwise, false.

异常

传递的索引超出了 0 到 FieldCount -1 的范围。The index passed was outside the range of 0 to FieldCount - 1.

尝试从已删除的行中检索数据。An attempt was made to retrieve data from a deleted row.

尝试读取或访问已关闭 DataTableReader 中的列。An attempt was made to read or access a column in a closed DataTableReader .

示例

下面的示例显示列中传入的编号为 2 的内容DataTableReaderThe following example displays the contents of the column numbered 2 within the passed-in DataTableReader. 如果值为 null 的特定行中的列,代码将显示文本<NULL >。If the value the column within a particular row is null, the code displays the text <NULL>. 如果正确类型,该示例的列中的数据显示每个行的错误消息。If the data within the column of the correct type, the example displays an error message for each row.

private static void PrintColumn(DataTableReader reader)
{
    // Loop through all the rows in the DataTableReader
    while (reader.Read())
    {
        if (reader.IsDBNull(2))
        {
            Console.Write("<NULL>");
        }
        else
        {
            try
            {
                Console.Write(reader.GetString(2));
            }
            catch (InvalidCastException)
            {
                Console.Write("Invalid data type.");
            }
        }
        Console.WriteLine();
    }
}
Private Sub PrintColumn(ByVal reader As DataTableReader)
   ' Loop through all the rows in the DataTableReader
   While reader.Read()
      If reader.IsDBNull(2) Then
         Console.Write("<NULL>")
      Else
         Try
            Console.Write(reader.GetString(2))
         Catch ex As InvalidCastException
            Console.Write("Invalid data type.")
         End Try
      End If
      Console.WriteLine()
   End While
End Sub

注解

调用此方法以查看是否存在 null 列的值在调用类型化的 get 方法 (例如, GetByteGetChar,依次类推) 以避免引发错误。Call this method to see if there are null column values before calling the typed get methods (for example, GetByte, GetChar, and so on) to avoid raising an error.

适用于