共用方式為


OracleDataReader.GetOrdinal(String) 方法

定義

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

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

參數

name
String

資料行名稱。

傳回

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

實作

範例

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

Public Sub ReadOracleData(ByVal connectionString As String)  

    Dim queryString As String = "SELECT OrderID, CustomerID FROM Orders"  
    Dim connection As New OracleConnection(connectionString)  
    Dim command As New OracleCommand(queryString, connectionString)  

    connection.Open()  

    Dim reader As OracleDataReader = command.ExecuteReader()  

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

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

    reader.Close()  
    connection.Close()  
End Sub  
public void ReadOracleData(string connectionString)  
{  
    string queryString = "SELECT OrderID, CustomerID FROM Orders";  
    OracleConnection connection = new OracleConnection(connectionString);  
    OracleCommand command = new OracleCommand(queryString,connection);  

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

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

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

    reader.Close();  
    connection.Close();  
}  

備註

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

GetOrdinal 不區分假名寬度。

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

適用於