Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

DataTableReader.GetOrdinal Method

Gets the column ordinal, given the name of the column.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)
public override int GetOrdinal(
	string name
)

Parameters

name
Type: System.String

The name of the column.

Return Value

Type: System.Int32
The zero-based column ordinal.

Implements

IDataRecord.GetOrdinal(String)
ExceptionCondition
InvalidOperationException

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

ArgumentException

The name specified is not a valid column name.

Because most of the methods provided by the DataTableReader class must be provided with an ordinal column number, you can use the GetOrdinal method to retrieve the column number, given the name of the column.

GetOrdinal performs a case-sensitive lookup first. If it fails, a second case-insensitive search is made. If the column number is not found an IndexOutOfRangeException is thrown.

GetOrdinal is kana-width insensitive.

Because ordinal-based lookups are more efficient than named lookups, it is inefficient to call GetOrdinal within a loop. Save time by calling GetOrdinal one time and assigning the results to an integer variable for use within the loop

If you have only a column name, in which case the column name is user supplied, and you must retrieve information from the column, you can use a procedure like the following to extract the required information. In this example, the procedure accepts a column name and returns the data that is contained within that column for the current row in the DataTableReader :

    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.  
        object columnValue;

        try
        {
            int columnOrdinal = reader.GetOrdinal(columnName);
            columnValue = reader.GetValue(columnOrdinal);
        }
        catch (ArgumentException ex)
        {
            // Throw all other errors back out to the caller.
            columnValue = null;
        }
        return columnValue;
    }

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.