OleDbDataReader.GetOrdinal(String) Metoda
Definicja
Pobiera numer porządkowy kolumny, uwzględniając nazwę kolumny.Gets the column ordinal, given the name of the column.
public:
override int GetOrdinal(System::String ^ name);
public:
virtual int GetOrdinal(System::String ^ name);
public override int GetOrdinal (string name);
public int GetOrdinal (string name);
override this.GetOrdinal : string -> int
abstract member GetOrdinal : string -> int
override this.GetOrdinal : string -> int
Public Overrides Function GetOrdinal (name As String) As Integer
Public Function GetOrdinal (name As String) As Integer
Parametry
- name
- String
Nazwa kolumny.The name of the column.
Zwraca
Numer porządkowy kolumny liczony od zera.The zero-based column ordinal.
Implementuje
Wyjątki
Podana nazwa nie jest prawidłową nazwą kolumny.The name specified is not a valid column name.
Przykłady
Poniższy przykład ilustruje sposób używania GetOrdinal metody.The following example demonstrates how to use the GetOrdinal method.
public static void ReadData(string connectionString)
{
string queryString = "SELECT DISTINCT CustomerID FROM Orders";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
int customerID = reader.GetOrdinal("CustomerID");
while (reader.Read())
{
Console.WriteLine("CustomerID={0}", reader.GetString(customerID));
}
reader.Close();
}
}
Public Sub ReadData(ByVal connectionString As String)
Dim queryString As String = "SELECT DISTINCT CustomerID FROM Orders"
Using connection As New OleDbConnection(connectionString)
Dim command As New OleDbCommand(queryString, connection)
connection.Open()
Dim reader As OleDbDataReader = command.ExecuteReader()
Dim customerID As Integer = reader.GetOrdinal("CustomerID")
While reader.Read()
Console.WriteLine("CustomerID={0}", reader.GetString(customerID))
End While
reader.Close()
End Using
End Sub
Uwagi
GetOrdinal wykonuje najpierw wyszukiwanie z uwzględnieniem wielkości liter.GetOrdinal performs a case-sensitive lookup first. Jeśli to się nie powiedzie, zostanie wykonane drugie wyszukiwanie bez uwzględniania wielkości liter.If it fails, a second case-insensitive search is made. Metoda zgłasza IndexOutOfRange
wyjątek, jeśli nie znaleziono numeru kolumny opartego na zero.The method throws an IndexOutOfRange
exception if the zero-based column ordinal is not found.
GetOrdinal ma nieuwzględnianie szerokości znaków kana.GetOrdinal is kana-width insensitive.
Ponieważ wyszukiwania oparte na liczbie porządkowej są wydajniejsze niż nazwane wyszukiwania, nie jest to efektywne GetOrdinal w przypadku wywołania wewnątrz pętli.Because ordinal-based lookups are more efficient than named lookups, it is inefficient to call GetOrdinal within a loop. Oszczędzaj czas, wywołując jednokrotnie GetOrdinal i przypisując wyniki do zmiennej całkowitej w celu użycia w pętli.Save time by calling GetOrdinal one time and assigning the results to an integer variable for use within the loop.