Reading result from DB reader

  • Thread starter Thread starter Jeremy H
  • Start date Start date
J

Jeremy H

Hello,

What is the nicest way to read Database data from reader objecy after making
a SQL query? Do I really need mapper classes with methods like:

// Reader is OleDbDataReader object.
public int GetInt(string column)
{
int data = (reader.IsDBNull(reader.GetOrdinal(column)))
? (int) 0 : (int)reader[column];
return data;
}

or does C#/dotNET provide any elegant solutions for checking null fields
etc. by it's own?

Br,
 
Well, if you are just going to cast via the indexer, you could use

Get<T>(string column) and replace all "int" with T

Personally I'd also call GetOrdinal once (at the top) to get the index, and
use this (rather than the string) to get the value
If you want specific versions I'd use reader.GetInt32(columnIndex)

Marc
 
But to answer your original question; not much more elegant than your
example unless I've just missed 'em.

Marc
 
Back
Top