DataTableReader.GetByte(Int32) 方法

定義

取得指定數據行的值做為位元組。

public:
 override System::Byte GetByte(int ordinal);
public override byte GetByte(int ordinal);
override this.GetByte : int -> byte
Public Overrides Function GetByte (ordinal As Integer) As Byte

參數

ordinal
Int32

零基列序數。

傳回

指定欄位的值。

例外狀況

通過的指數超出0到 FieldCount -1的範圍。

嘗試從已刪除的資料列中取得資料。

嘗試讀取或存取封閉 DataTableReader.

指定的欄位不包含位元組。

範例

以下範例顯示了在 傳入 DataTableReader中編號為 2 的欄位內容。 如果某列中的欄位值為 null,程式碼會顯示文字 <NULL。> 若欄位內的資料類型不正確,範例會顯示每列錯誤訊息。

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.GetByte(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.GetByte(2))
         Catch ex As InvalidCastException
            Console.Write("Invalid data type.")
         End Try
      End If
      Console.WriteLine()
   End While
End Sub

備註

不進行任何轉換;因此,取出的資料必須已經是位元組或可強制於位元組。

呼叫此方法前,先呼叫 IsDBNull 確認是否有空值。

適用於