DataTableReader.IsDBNull(Int32) 方法

定义

获取一个值,该值指示列中是否包含不存在的或缺少的值。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

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

返回

如果指定的列值与 true 等效,则为 DBNull;否则为 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 .

示例

下面的示例在传入的 DataTableReader中显示编号为2的列的内容。The following example displays the contents of the column numbered 2 within the passed-in DataTableReader. 如果特定行中的列的值为 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

注解

调用此方法可在调用类型化 get 方法之前(例如 GetByteGetChar等)查看是否存在 null 列值,以避免引发错误。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.

适用于