DataTableReader.Item[] 屬性

定義

取得指定欄位的本位格式值。

多載

名稱 Description
Item[Int32]

取得指定資料行的原生格式,指定資料行序數的值。

Item[String]

取得指定數據行名稱的原生格式指定數據行的值。

Item[Int32]

取得指定資料行的原生格式,指定資料行序數的值。

public:
 virtual property System::Object ^ default[int] { System::Object ^ get(int ordinal); };
public override object this[int ordinal] { get; }
member this.Item(int) : obj
Default Public Overrides ReadOnly Property Item(ordinal As Integer) As Object

參數

ordinal
Int32

零基列序數。

屬性值

指定欄位的本體格式值。

例外狀況

通過的指數超出0到 FieldCount -1的範圍。

範例

以下範例顯示所有欄位的內容,涵蓋所有列,來自提供的 DataTableReader。 程式碼使用 Item[] 方法(Microsoft C# 中的索引器)來取得每欄所包含的值。

private static void DisplayItems(DataTableReader reader)
{
    int rowNumber = 0;
    while (reader.Read())
    {
        Console.WriteLine("Row " + rowNumber);
        for (int i = 0; i < reader.FieldCount; i++)
        {
            Console.WriteLine("{0}: {1}", reader.GetName(i), reader[i]);
        }
        rowNumber++;
    }
}
Private Sub DisplayItems(ByVal reader As DataTableReader)
   Dim rowNumber As Integer
   While reader.Read()
      Console.WriteLine("Row " & rowNumber)
      For i As Integer = 0 To reader.FieldCount - 1
         Console.WriteLine("{0}: {1}", reader.GetName(i), reader.Item(i))
      Next
      rowNumber += 1
   End While
End Sub

備註

此過載 Item[] 的行為與方法 GetValue 相同。

另請參閱

適用於

Item[String]

取得指定數據行名稱的原生格式指定數據行的值。

public:
 virtual property System::Object ^ default[System::String ^] { System::Object ^ get(System::String ^ name); };
public override object this[string name] { get; }
member this.Item(string) : obj
Default Public Overrides ReadOnly Property Item(name As String) As Object

參數

name
String

欄位名稱。

屬性值

指定欄位的本體格式值。

例外狀況

所指定的名稱並非有效的欄位名稱。

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

嘗試讀取或存取封閉 DataTableReader.

範例

給定 a DataTableReader 和欄位名稱,GetValueByName 程序會回傳指定欄位的值。 在呼叫此程序之前,您必須建立一個新 DataTableReader 實例,並至少呼叫一次其 Read 方法,以將列指標定位在資料列上。

private static object GetValueByName(
    DataTableReader reader, string columnName)
{
    // Consider when to use a procedure like this one carefully:
    // if  you're going to retrieve information from a column
    // in a loop, it would be better to retrieve the column
    // ordinal once, store the value, and use the methods
    // of the DataTableReader class directly.
    // Use this string-based indexer sparingly.
    object columnValue = null;

    try
    {
        columnValue = reader[columnName];
    }
    catch (ArgumentException ex)
    {
        // Throw all other errors back out to the caller.
        columnValue = null;
    }
    return columnValue;
}
Private Function GetValueByName( _
   ByVal reader As DataTableReader, _
   ByVal columnName As String) As Object

   ' Consider when to use a procedure like this one carefully:
   ' If you're going to retrieve information from a column
   ' in a loop, it would be better to retrieve the column
   ' ordinal once, store the value, and use the methods
   ' of the DataTableReader class directly. 
   ' Use Item(columnName) sparingly.
   Dim columnValue As Object

   Try
      columnValue = reader.Item(columnName)
   Catch ex As ArgumentException
      ' Throw all other errors back out to the caller.
      columnValue = Nothing
   End Try
   Return columnValue
End Function

備註

首先會執行大小寫區分查詢。 若失敗,則進行第二次不區寫搜尋。

這種方法對假名寬度不敏感。

這個超載版本 Item[] 對應於先呼叫該 GetOrdinal 方法,然後再呼叫該 GetValue 方法。

適用於