This documentation is archived and is not being maintained.

IDataRecord.GetOrdinal Method

Return the index of the named field.

[Visual Basic]
Function GetOrdinal( _
   ByVal name As String _
) As Integer
[C#]
int GetOrdinal(
   string name
);
[C++]
int GetOrdinal(
   String* name
);
[JScript]
function GetOrdinal(
   name : String
) : int;

Parameters

name
The name of the field to find.

Return Value

The index of the named field.

Remarks

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

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 once and assigning the results to an integer variable for use within the loop.

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the inherited GetOrdinal method.

[Visual Basic] 
Public Sub ReadMyData(myConnString As String)

  Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
  Dim myConnection As New OdbcConnection(myConnString)
  Dim myCommand As New OdbcCommand(mySelectQuery, myConnection)

  myConnection.Open()

  Dim myReader As OdbcDataReader = myCommand.ExecuteReader()

  Dim custIdCol As Integer = myReader.GetOrdinal("CustomerID")

  Do While myReader.Read()
    Console.WriteLine("CustomerID = {0}", myReader.GetString(custIdCol))
  Loop

  myReader.Close()
  myConnection.Close()
End Sub

[C#] 
public void ReadMyData(string myConnString)
{
    string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
    OdbcConnection myConnection = new OdbcConnection(myConnString);
    OdbcCommand myCommand = new OdbcCommand(mySelectQuery,myConnection);

    myConnection.Open();
    OdbcDataReader myReader = myCommand.ExecuteReader();

    int custIdCol = myReader.GetOrdinal("CustomerID");

    while (myReader.Read())
       Console.WriteLine("CustomerID = {0}", myReader.GetString(custIdCol));

    myReader.Close();
    myConnection.Close();
}

[C++] 
public:
    void ReadMyData(String* myConnString)
    {
        String* mySelectQuery = S"SELECT OrderID, CustomerID FROM Orders";
        OdbcConnection* myConnection = new OdbcConnection(myConnString);
        OdbcCommand* myCommand = new OdbcCommand(mySelectQuery, myConnection);

        myConnection->Open();
        OdbcDataReader* myReader = myCommand->ExecuteReader();

        int custIdCol = myReader->GetOrdinal(S"CustomerID");

        while (myReader->Read())
            Console::WriteLine(S"CustomerID = {0}", myReader->GetString(custIdCol));

        myReader->Close();
        myConnection->Close();
    };

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

IDataRecord Interface | IDataRecord Members | System.Data Namespace

Show: