DataTableReader.Item[] 屬性

定義

取得使用原生格式的指定資料行值。

多載

Item[Int32]

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

Item[String]

提供資料行名稱,取得使用原生格式的指定資料行值。

Item[Int32]

來源:
DataTableReader.cs
來源:
DataTableReader.cs
來源:
DataTableReader.cs

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

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]

來源:
DataTableReader.cs
來源:
DataTableReader.cs
來源:
DataTableReader.cs

提供資料行名稱,取得使用原生格式的指定資料行值。

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 中讀取或存取資料行。

範例

指定 和 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 方法。

適用於