A neater way of getting data out of a DbDataReader

Previously I have always done the following to iterate through a bDataReader

while(reader.Read())
{
int index = reader.GetOrdinal(“User_Description”);
list.Add(reader[index] as string);
}
But today I stumbled upon the following which is so much neater:

foreach (DbDataRecord dbDataRecord in reader)
{
int index = dbDataRecord.GetOrdinal(“User_Description”);
list.Add(dbDataRecord[index] as string);
}

I just think it’s a lot neater and clearer as to what’s going on.

Tagged . Bookmark the permalink.

Comments are closed.