DataReader - hardcode ordinals?

Posted by David Neale on Stack Overflow See other posts from Stack Overflow or by David Neale
Published on 2010-05-21T13:07:48Z Indexed on 2010/05/21 13:10 UTC
Read the original article Hit count: 505

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?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#