Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using OleDb to find a column with a given name

Posted on 2006-07-13
5
Medium Priority
?
253 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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