DataTableReader.GetInt16(Int32) 方法

定義

取得指定資料行的值做為 16 位元帶正負號的整數。

public:
 override short GetInt16(int ordinal);
public override short GetInt16 (int ordinal);
override this.GetInt16 : int -> int16
Public Overrides Function GetInt16 (ordinal As Integer) As Short

參數

ordinal
Int32

以零為基底的資料行序數。

傳回

指定的資料行值。

例外狀況

傳遞的索引超出 0 到 FieldCount - 1 的範圍。

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

嘗試在關閉的 DataTableReader 中讀取或存取資料行。

指定的資料行不包含 16 位元帶正負號的整數。

範例

下列範例會顯示傳入 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.GetInt16(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.GetInt16(2))
         Catch ex As InvalidCastException
            Console.Write("Invalid data type.")
         End Try
      End If
      Console.WriteLine()
   End While
End Sub

備註

不會執行任何轉換;因此,擷取的數據必須已經是 Int16 或 強制使用 Int16

呼叫 IsDBNull 以查看在呼叫這個方法之前是否有 Null 值。

適用於