DataTableReader.GetDouble(Int32) 方法

定義

取得資料行的值做為雙精確度浮點數。

public:
 override double GetDouble(int ordinal);
public override double GetDouble (int ordinal);
override this.GetDouble : int -> double
Public Overrides Function GetDouble (ordinal As Integer) As Double

參數

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.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

備註

不會執行任何轉換;因此,擷取的數據必須已經是雙精確度浮點數,或必須強制轉換為雙精確度浮點數。

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

適用於