DataTableReader.GetOrdinal(String) 메서드

정의

열 이름이 지정된 경우 열 서수를 가져옵니다.

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

매개 변수

name
String

열 이름입니다.

반환

열 서수(0부터 시작)입니다.

예외

닫힌 DataTableReader의 열을 읽거나 액세스하려고 한 경우

지정된 이름이 올바른 열 이름이 아닌 경우

예제

열 이름만 경우 경우에서 열 이름이 사용자가 제공 하 고 열에서 정보를 검색 해야 합니다, 그리고 다음과 같은 절차를 사용 하 여 필요한 정보를 추출 하 합니다. 이 예제에서는 절차 열 이름을 허용 하 고의 현재 행에 대해 해당 열에 포함 된 데이터를 반환 합니다 DataTableReader :

private static object GetValueByName(
    DataTableReader reader, string columnName)
{
    // Consider when to use a procedure like this one carefully:
    // if you're going to retrieve information from a column
    // in a loop, it would be better to retrieve the column
    // ordinal once, store the value, and use the methods
    // of the DataTableReader class directly.
    object columnValue;

    try
    {
        int columnOrdinal = reader.GetOrdinal(columnName);
        columnValue = reader.GetValue(columnOrdinal);
    }
    catch (ArgumentException ex)
    {
        // Throw all other errors back out to the caller.
        columnValue = null;
    }
    return columnValue;
}
Private Function GetValueByName( _
   ByVal reader As DataTableReader, _
   ByVal columnName As String) As Object

   ' Consider when to use a procedure like this one carefully:
   ' If you're going to retrieve information from a column
   ' in a loop, it would be better to retrieve the column
   ' ordinal once, store the value, and use the methods
   ' of the DataTableReader class directly. 
   Dim columnValue As Object

   Try
      Dim columnOrdinal As Integer = reader.GetOrdinal(columnName)
      columnValue = reader.GetValue(columnOrdinal)
   Catch ex As ArgumentException
      ' Throw all other errors back out to the caller.
      columnValue = Nothing
   End Try
   Return columnValue
End Function

설명

대부분의 메서드를 제공 하므로 합니다 DataTableReader 클래스는 서 수 열 번호와 함께 사용할 수 있습니다 제공 해야 합니다.는 GetOrdinal 열 이름이 지정 된 열 번호를 검색 하는 방법입니다.

GetOrdinal 대/소문자 구분 조회를 먼저 수행합니다. 실패 한 경우에 두 번째는 대/소문자 검색이 수행 됩니다. 열 번호를 찾을 수 없으면는 IndexOutOfRangeException throw 됩니다.

GetOrdinal에서는 가나 너비를 구분하지 않습니다.

서수 기반 조회가 명명된 조회보다 효율적이므로 루프 내에서 GetOrdinal을 호출하는 것은 비효율적입니다. 호출 하 여 시간을 절약할 GetOrdinal 번 및 루프 내에서 사용 하 여 정수 변수에 결과 할당

적용 대상