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 는 대/소문자를 구분하는 조회를 먼저 수행합니다. 실패하면 대/소문자를 구분하지 않는 두 번째 검색이 이루어집니다. 열 번호를 찾을 수 없으면 throw ArgumentException 됩니다.

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

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

적용 대상