DataTableReader.GetBoolean(Int32) 方法

定义

获取指定列的 Boolean 形式的值。Gets the value of the specified column as a Boolean.

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

参数

ordinal
Int32

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

返回

Boolean

指定列的值。The value of the specified column.

例外

传递的索引超出了 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.

指定的列不包含 BooleanThe specified column does not contain a Boolean.

示例

下面的示例在传入的中显示编号为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 is not 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.GetBoolean(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.GetBoolean(2))
         Catch ex As InvalidCastException
            Console.Write("Invalid data type.")
         End Try
      End If
      Console.WriteLine()
   End While
End Sub

注解

不执行任何转换;因此,检索的数据必须已是布尔值或强制转换为布尔值,否则将生成异常。No conversions are performed; therefore the data retrieved must either already be a Boolean or coercible to a Boolean, or an exception is generated.

调用 IsDBNull 此方法之前,请调用以查看是否存在 null 值。Call IsDBNull to see if there are null values before calling this method.

适用于