共用方式為


IDataRecord.GetOrdinal(String) 方法

定義

傳回具名欄位的索引。

public:
 int GetOrdinal(System::String ^ name);
public int GetOrdinal (string name);
abstract member GetOrdinal : string -> int
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 (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 會先執行區分大小寫的查閱。 如果失敗,則會進行第二個不區分大小寫的搜尋。 GetOrdinal 不區分假名寬度。 如果找不到具名字段的索引, IndexOutOfRangeException 則會擲回 。

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

適用於