469,572 Members | 1,307 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

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 1671
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by andreas.maurer1971 | last post: by
10 posts views Thread by Neil | last post: by
5 posts views Thread by Mark Feller | last post: by
136 posts views Thread by Matt Kruse | last post: by
6 posts views Thread by kamsmartx | last post: by
4 posts views Thread by trullock | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.