Thursday, February 12, 2009

DataReader.ToInt32(0) versus Convert.ToInt32(dataReader[0])

DataReader.GetInt32() will not perform any type conversions; therefore, the data retrieved must already be a 32-bit signed integer. Convert.ToInt32() will be happy to convert from a double, for example.

I'd tend to err towards the stricter approach to avoid unforeseen casting issues. I.e. something changes in the database.

Checks for IsDBNull() may also be required.

You will also need to consider how to reference the columns. By name or by ordinal position. See Roughly 3% penalty for indexing SqlDataReader columns by string rather than int.