Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 1.1
.NET Framework
Reference
System.Data.OleDb
Methods
 GetOrdinal Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2003/.NET Framework 1.1

Other versions are also available for the following:
.NET Framework Class Library
OleDbDataReader.GetOrdinal Method

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

[Visual Basic]
Public Overridable Function GetOrdinal( _
   ByVal name As String _
) As Integer Implements IDataRecord.GetOrdinal
[C#]
public virtual int GetOrdinal(
 string name
);
[C++]
public: virtual int GetOrdinal(
 String* name
);
[JScript]
public function GetOrdinal(
   name : String
) : int;

Parameters

name
The name of the column.

Return Value

The zero-based column ordinal.

Implements

IDataRecord.GetOrdinal

Exceptions

Exception Type Condition
IndexOutOfRangeException The name specified is not a valid column name.

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 GetOrdinal method.

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

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

  myConnection.Open()

  Dim myReader As OleDbDataReader = 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";
    OleDbConnection myConnection = new OleDbConnection(myConnString);
    OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection);

    myConnection.Open();
    OleDbDataReader 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";
        OleDbConnection* myConnection = new OleDbConnection(myConnString);
        OleDbCommand* myCommand = new OleDbCommand(mySelectQuery, myConnection);

        myConnection->Open();
        OleDbDataReader* 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

See Also

OleDbDataReader Class | OleDbDataReader Members | System.Data.OleDb Namespace | GetName

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker