473,421 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

Best way to retrieve more than one field in a single record

Hi, I am looking for the recommended way to retrieve several values
from a single record, i.e. a typical lookup scenario. An example would
be a query that needs to retrieve user_name, user_addres, user_email
for a given user.
I know that if I had to get a single values I should use the
Command.ExecuteScalar() and that if I need to get several records I
should use the SqlDataReader and loop using its Read() method.
But how should I retrieve multiple values from a single record?
Thank you for your help.
ilZoro.

May 2 '06 #1
6 1807
One route is ExecuteNonQuery using variables / parameters for the items

Marc
May 2 '06 #2
Either do Read() once or follow Marc's suggestion.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"zoro" <il****@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Hi, I am looking for the recommended way to retrieve several values
from a single record, i.e. a typical lookup scenario. An example would
be a query that needs to retrieve user_name, user_addres, user_email
for a given user.
I know that if I had to get a single values I should use the
Command.ExecuteScalar() and that if I need to get several records I
should use the SqlDataReader and loop using its Read() method.
But how should I retrieve multiple values from a single record?
Thank you for your help.
ilZoro.

May 2 '06 #3
Thanks Marc, Miha. So far i did both but felt there must be a 'proper'
way to do it, built in the ADO.NET architecture - just like
ExecuteScalar(). Perhaps ExecuteRecord() ?

May 2 '06 #4
"zoro" wrote:
Thanks Marc, Miha. So far i did both but felt there must be a 'proper'
way to do it, built in the ADO.NET architecture - just like
ExecuteScalar(). Perhaps ExecuteRecord() ?


I think there may be some confusion. SqlDataReader.Read() returns a record
for each row returned by your query. Just call SqlDataReader.GetString(x) (or
which ever method maps to the appropriate data type), replacing x with either
the field ordinal returned by SqlDataReader.GetOrdinal("fieldName"), or the
name of the field you want.

May 2 '06 #5
Thanks Dan. You are right of course to say that what I'm trying to
achieve can be achieved in several other ways. I suppose what I am
trying to say is that if ADO.NET provides a special way to retrieve a
single value with ExecuteScalar() (even though you could achieve this
task using SqlDataReader), in order to handle a popular type of query,
it stands to reason it would also provide a special way to do this
common task too.

May 2 '06 #6
zoro wrote:

[...snip...]
if ADO.NET provides a special way to retrieve a
single value with ExecuteScalar() (even though you could achieve this
task using SqlDataReader), in order to handle a popular type of query,
it stands to reason it would also provide a special way to do this
common task too.


If you select a scalar, you'd not expect anything else except a single
value. If you do a "select * from table where condition = true", it's very
likely the result will have more than one row (even though you might be sute
it will contain one row only). Since you cannot know in advance the number
of rows your query will return, there is no sense in having some method
returning one row only...
May 2 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: andreas.maurer1971 | last post by:
Hi all, since a few years I use the following statement to find duplicate entries in a table: SELECT t1.id, t2.id,... FROM table AS t1 INNER JOIN table AS t2 ON t1.field = t2.field WHERE...
10
by: Neil | last post by:
I need to get two values from a complex SQL statement which returns a single record and use those two values to update a single record in a table. In order to assign those two values to variables...
5
by: Mark Feller | last post by:
I want to provide users a page where they can browse entries in a database 10 at a time, for example. I am doing this as a table, where each row is a database entry. I want to be able to give the...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
1
by: Robin S. | last post by:
More clueless newbie questions... Table 1: "available specs" Table 2: "classes" A single form shows a single record from the table "classes". Each record in "classes" may contain several...
1
by: Chris Uwins | last post by:
Hi there, i know theres a number of ways I can achieve this but want to know the best, (but still quite simple). Up until a year ago I never used Access but have designed a few databases for...
13
by: kev | last post by:
Hi all, I have created a database for equipments. I have a form to register the equipment meaning filling in all the particulars (ID, serial, type, location etc). I have two buttons at the end...
6
by: kamsmartx | last post by:
I'm new to programming and need some help figuring out an algorithm. I need to design some kind of algorithm which will help us define capacity for one of our portfolios....here's the problem...
4
by: trullock | last post by:
Hi, Can anyone suggest the best way to go about the following... I'm tracking clicks (mouse down x,y coordinates) on a web page by using some javascript to create an XHR which sends the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.