DataReader - hardcode ordinals?
- by David Neale
When returning data from a DataReader I would typically use the ordinal reference on the DataReader to grab the relevant column:
if (dr.HasRows)
Console.WriteLine(dr[0].ToString());
(OR dr.GetString(0); OR (string)dr[0];)...
I have always done this because I was advised at an early stage that using dr["ColumnName"] or a more elegant way of indexing causes a performance hit.
However, whilst everything is becoming increasingly strongly-typed I feel more uncomfortable with this. I'm also aware that the above does not check for DBNull.
How should data be returned from a DataReader?