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 確認是否有空值。

適用於