Solved

Using OleDb to find a column with a given name

Posted on 2006-07-13
5
243 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

628 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