SqlDataReader.GetOrdinal(String) 方法

定義

提供資料行的名稱,取得資料行序數。Gets the column ordinal, given the name of the column.

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

參數

name
String

資料行名稱。The name of the column.

傳回

以零為基底的資料行序數。The zero-based column ordinal.

實作

例外狀況

指定的名稱不是有效的資料行名稱。The name specified is not a valid column name.

範例

下列範例示範如何使用 GetOrdinal 方法。The following example demonstrates how to use the GetOrdinal method.

private static void ReadGetOrdinal(string connectionString)
{
    string queryString = "SELECT DISTINCT CustomerID FROM dbo.Orders;";
    using (SqlConnection connection =
               new SqlConnection(connectionString))
    {
        SqlCommand command =
            new SqlCommand(queryString, connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        // Call GetOrdinal and assign value to variable.
        int customerID = reader.GetOrdinal("CustomerID");

        // Use variable with GetString inside of loop.
        while (reader.Read())
        {
            Console.WriteLine("CustomerID={0}", reader.GetString(customerID));
        }

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

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        connection.Open()

        Dim reader As SqlDataReader = command.ExecuteReader()

        ' Call GetOrdinal and assign value to variable.
        Dim customerID As Integer = reader.GetOrdinal("CustomerID")

        ' Use variable with GetString inside of loop.
        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 performs a case-sensitive lookup first. 如果失敗,則會發生第二個、不區分大小寫的搜尋(使用資料庫定序來進行不區分大小寫的比較)。If it fails, a second, case-insensitive search occurs (a case-insensitive comparison is done using the database collation). 當比較因文化特性而異的大小寫規則而受到影響時,可能會產生非預期結果。Unexpected results can occur when comparisons are affected by culture-specific casing rules. 例如,在土耳其文中,下列範例會產生錯誤的結果,因為土耳其文中的檔案系統不會在 "file" 中使用字母 ' i ' 的語言大小寫規則。For example, in Turkish, the following example yields the wrong results because the file system in Turkish does not use linguistic casing rules for the letter 'i' in "file". 如果找不到以零為基底的資料行序數,方法會擲回 IndexOutOfRange 例外狀況。The method throws an IndexOutOfRange exception if the zero-based column ordinal is not found.

GetOrdinal 不區分假名-寬度。GetOrdinal is kana-width insensitive.

由於以序數為基礎的查閱比命名查閱更有效率,因此在迴圈內呼叫 GetOrdinal 沒有效率。Because ordinal-based lookups are more efficient than named lookups, it is inefficient to call GetOrdinal within a loop. 藉由呼叫 GetOrdinal 一次,並將結果指派給整數變數以在迴圈內使用,以節省時間。Save time by calling GetOrdinal once and assigning the results to an integer variable for use within the loop.

適用於

另請參閱