У меня был тот же вопрос себе, и я нашел два общие ответы были:
- Алиас поле в вашем SQL
- Используйте целочисленный индекс для столбца
Мне не понравился ни один из этих вариантов, поэтому я создал третью: GetNthOrdinal.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
public static class SqlDataReaderExtensions
{
public static int GetNthOrdinal(this SqlDataReader reader, string columnName, int nthOccurrence = 1)
{
// Get the schema which represents the columns in the reader
DataTable schema = reader.GetSchemaTable();
// Find all columns in the schema which match the name we're looking for.
// schema is a table and each row is a column from our reader.
var occurrences = schema.Rows.Cast<DataRow>().Where(r => string.Equals((string)r["ColumnName"], columnName, StringComparison.Ordinal));
// Get the nthOccurrence. Will throw if occurrences is empty.
// reader.GetOrdinal will also throw if a column is not present, but you may want to
// have this throw a more meaningful exception
var occurrence = occurrences.Skip(nthOccurrence - 1).First();
// return the ordinal
return (int)occurrence["ColumnOrdinal"];
}
}
Использование:
reader.GetNthOrdinal("Id", 2);
Важно отметить, что п-й появление не 0 на основе; он начинается с 1.
Зачем вам вообще нужно имя? Вы можете использовать 'reader.GetInt32 (0)' и 'reader.GetInt32 (1)'. –