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 override object this[int ordinal] { get; }

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++;
    }
}

Remarks

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

See also

Applies to

.NET 9 ja muud versioonid
Toode Versioonid
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

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 override object this[string name] { get; }

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;
}

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

.NET 9 ja muud versioonid
Toode Versioonid
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1