共用方式為


OdbcDataReader.GetOrdinal(String) 方法

定義

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

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

參數

value
String

資料行名稱。

傳回

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

實作

範例

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

public static void ReadData(string connectionString)
{
    string queryString = "SELECT DISTINCT CustomerID FROM Orders";

    using (OdbcConnection connection = new OdbcConnection(connectionString))
    {
        OdbcCommand command = new OdbcCommand(queryString, connection);

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

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

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

        // Call Close when done reading.
        reader.Close();
    }
}
Public Sub ReadData(ByVal connectionString As String)
    Dim queryString As String = "SELECT DISTINCT CustomerID FROM Orders"

    Using connection As New OdbcConnection(connectionString)
        Dim command As New OdbcCommand(queryString, connection)

        connection.Open()

        Dim reader As OdbcDataReader = command.ExecuteReader()

        Dim customerID As Integer = reader.GetOrdinal("CustomerID")

        While reader.Read()
            Console.WriteLine("CustomerID={0}", reader.GetString(customerID))
        End While

        ' Call Close when done reading.
        reader.Close()
    End Using
End Sub

備註

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

GetOrdinal 不區分假名寬度。

因為根據序數進行查閱比具名查閱更有效率,所以在迴圈內呼叫 GetOrdinal 的效率不佳。 請改為呼叫 GetOrdinal 一次,然後將結果指派給整數變數,以在迴圈中使用。

適用於

另請參閱