Reading result from DB reader

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeremy H

    Reading result from DB reader

    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.IsDBNul l(reader.GetOrd inal(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,


  • Marc Gravell

    #2
    Re: Reading result from DB reader

    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


    Comment

    • Marc Gravell

      #3
      Re: Reading result from DB reader

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

      Marc


      Comment

      Working...