Solved

Using OleDb to find a column with a given name

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

837 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