Solved

Using OleDb to find a column with a given name

Posted on 2006-07-13
5
234 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:ba272
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17101603
Hi, you can use the readers indexer and specify the columns name:

int id = int.Parse(reader["ID"].ToString());
0
 
LVL 13

Accepted Solution

by:
devsolns earned 500 total points
ID: 17101624
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
 

Author Comment

by:ba272
ID: 17101644
Thanks.
0
 

Author Comment

by:ba272
ID: 17102255
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
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 17102887
Hey, no probs at all! devsolns provided a better method in the end, so its cool!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question