DataTableReader.GetDecimal(Int32) 方法

定義

取得指定的資料行值做為 Decimal

public:
 override System::Decimal GetDecimal(int ordinal);
public override decimal GetDecimal (int ordinal);
override this.GetDecimal : int -> decimal
Public Overrides Function GetDecimal (ordinal As Integer) As Decimal

參數

ordinal
Int32

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

傳回

指定的資料行值。

例外狀況

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

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

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

指定的資料行不包含 Decimal 值。

範例

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

備註

不會執行轉換;因此,擷取的數據必須已經是 Decimal 或可強制轉換為 Decimal

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

適用於