DataTableReader.GetString(Int32) 方法

定義

取得指定的資料行值做為字串。

public:
 override System::String ^ GetString(int ordinal);
public override string GetString (int ordinal);
override this.GetString : int -> string
Public Overrides Function GetString (ordinal As Integer) As String

參數

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

備註

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

適用於