Using OleDb to find a column with a given name

Hi,

I am designing for an SQL Server database, but was provided an Access mock up because the SQL Server database is not yet done.  So I need to make my code as transportable as possible.

How do I "request" the column name from an OleDbReader?  Or is there some other way to get the data from the first column, for example, without already knowing that the ID is stored in the first column.  In this example, the first column is called "ID".

int id = reader.GetInt32(0);

This capability would allow me to switch to the SQL Server version more easily, since I expect the columns to be in a different order.


Thanks,
Bob
ba272Asked:
Who is Participating?
 
devsolnsConnect With a Mentor Commented:
or

int id = reader.GetInt32(reader.GetOrdinal("ID"))

would easily be the most efficient.


it really disappoints me that those methods arent over loaded....
0
 
Ravi SinghSenior Software EngineerCommented:
Hi, you can use the readers indexer and specify the columns name:

int id = int.Parse(reader["ID"].ToString());
0
 
ba272Author Commented:
Thanks.
0
 
ba272Author Commented:
Zephyr.

I'm sorry I didn't see your post.  When I got a reply and clicked the link the browser I saw one, and I mistakenly did't look above.  It had come in so quickly after my post, I presumed there was just one reply.

Thanks for the help you always provide me.


Sorry,
Bob
0
 
Ravi SinghSenior Software EngineerCommented:
Hey, no probs at all! devsolns provided a better method in the end, so its cool!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.