Uredi

Deli z drugimi prek


DataTableReader.Item[] Property

Definition

Gets the value of the specified column in its native format.

Overloads

Item[Int32]

Gets the value of the specified column in its native format given the column ordinal.

Item[String]

Gets the value of the specified column in its native format given the column name.

Item[Int32]

Source:
DataTableReader.cs
Source:
DataTableReader.cs
Source:
DataTableReader.cs

Gets the value of the specified column in its native format given the column ordinal.

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

Parameters

ordinal
Int32

The zero-based column ordinal.

Property Value

The value of the specified column in its native format.

Exceptions

The index passed was outside the range of 0 to FieldCount - 1.

Examples

The following example displays the contents of all the columns, in all the rows from the supplied DataTableReader. The code uses the Item[] method (the indexer, in Microsoft C#) to retrieve the value that is contained in each column.

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

Remarks

This overload for Item[] behaves identically to the GetValue method.

See also

Applies to

Item[String]

Source:
DataTableReader.cs
Source:
DataTableReader.cs
Source:
DataTableReader.cs

Gets the value of the specified column in its native format given the column name.

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

Parameters

name
String

The name of the column.

Property Value

The value of the specified column in its native format.

Exceptions

The name specified is not a valid column name.

An attempt was made to retrieve data from a deleted row.

An attempt was made to read or access a column in a closed DataTableReader.

Examples

Given a DataTableReader and a column name, the GetValueByName procedure returns the value of the specified column. Before calling this procedure, you must create a new DataTableReader instance and call its Read method at least one time to position the row pointer on a row of data.

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

Remarks

A case-sensitive lookup is performed first. If it fails, a second case-insensitive search is made.

This method is kana-width insensitive.

This overloaded version of Item[] corresponds to calling the GetOrdinal method, and then subsequently calling the GetValue method.

Applies to