共用方式為


OleDbDataReader.GetOrdinal(String) 方法

定義

提供資料行的名稱,取得資料行序數。

public:
 override int GetOrdinal(System::String ^ name);
public:
 virtual int GetOrdinal(System::String ^ name);
public override int GetOrdinal (string name);
public int GetOrdinal (string name);
override this.GetOrdinal : string -> int
abstract member GetOrdinal : string -> int
override this.GetOrdinal : string -> int
Public Overrides Function GetOrdinal (name As String) As Integer
Public Function GetOrdinal (name As String) As Integer

參數

name
String

資料行名稱。

傳回

以零為基底的資料行序數。

實作

例外狀況

指定的名稱不是有效的資料行名稱。

範例

下列範例示範如何使用 GetOrdinal 方法。

public static void ReadData(string connectionString)
{
    string queryString = "SELECT DISTINCT CustomerID FROM Orders";
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand(queryString, connection);

        connection.Open();
        OleDbDataReader reader = command.ExecuteReader();

        int customerID = reader.GetOrdinal("CustomerID");

        while (reader.Read())
        {
            Console.WriteLine("CustomerID={0}", reader.GetString(customerID));
        }
        reader.Close();
    }
}
Public Sub ReadData(ByVal connectionString As String)
    Dim queryString As String = "SELECT DISTINCT CustomerID FROM Orders"

    Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand(queryString, connection)

        connection.Open()

        Dim reader As OleDbDataReader = command.ExecuteReader()

        Dim customerID As Integer = reader.GetOrdinal("CustomerID")
        While reader.Read()
            Console.WriteLine("CustomerID={0}", reader.GetString(customerID))
        End While
        reader.Close()
    End Using
End Sub

備註

GetOrdinal 會先執行區分大小寫的查閱。 如果失敗,則會進行第二個不區分大小寫的搜尋。 如果找不到以零起始的數據行序數,方法會 IndexOutOfRange 擲回例外狀況。

GetOrdinal 不區分假名寬度。

因為根據序數進行查閱比具名查閱更有效率,所以在迴圈內呼叫 GetOrdinal 的效率不佳。 藉由呼叫 GetOrdinal 一次,並將結果指派給整數變數,以在迴圈內使用,以節省時間。

適用於

另請參閱