471,570 Members | 927 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

Reading result from DB reader


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?

Jan 26 '06 #1
2 1733
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)

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

Jan 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.