DataReader and JOIN query

D

dejavue82

This is the result of a JOINed query:

question answer

1 a1
1 a2
1 a3
1 a4
2 a1
2 a2
2 a3
2 a4

How would I use a DataReader to output this data without repeating each
question 4 times?

Do I weed out the repeated question in C# with a while loop?
Or is there a way to do this in the SQL statement?

Thank you for your time and help!
 
D

dejavue82

I simply want to output 4 answers for every question without repeating
the question.
(with a datareader if possible)




Eliyahu said:
Give an example of what you would like to see in the result record set.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


This is the result of a JOINed query:

question answer

1 a1
1 a2
1 a3
1 a4
2 a1
2 a2
2 a3
2 a4

How would I use a DataReader to output this data without repeating each
question 4 times?

Do I weed out the repeated question in C# with a while loop?
Or is there a way to do this in the SQL statement?

Thank you for your time and help!
 
E

Eliyahu Goldin

Like this?

1 a1
a2
a3
a4
2 a1
a2
a3
a4


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


I simply want to output 4 answers for every question without repeating
the question.
(with a datareader if possible)




Eliyahu said:
Give an example of what you would like to see in the result record set.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


This is the result of a JOINed query:

question answer

1 a1
1 a2
1 a3
1 a4
2 a1
2 a2
2 a3
2 a4

How would I use a DataReader to output this data without repeating each
question 4 times?

Do I weed out the repeated question in C# with a while loop?
Or is there a way to do this in the SQL statement?

Thank you for your time and help!
 
D

dejavue82

Exactly! But I'm having trouble writing the SQL code for this. Also,
this kind of model wouldn't work well with DataReader, since it read
one row at a time.

Would a DataSet be better here? (easier, perhaps?)


Eliyahu said:
Like this?

1 a1
a2
a3
a4
2 a1
a2
a3
a4


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


I simply want to output 4 answers for every question without repeating
the question.
(with a datareader if possible)




Eliyahu said:
Give an example of what you would like to see in the result record set.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


This is the result of a JOINed query:

question answer

1 a1
1 a2
1 a3
1 a4
2 a1
2 a2
2 a3
2 a4

How would I use a DataReader to output this data without repeating each
question 4 times?

Do I weed out the repeated question in C# with a while loop?
Or is there a way to do this in the SQL statement?

Thank you for your time and help!
 
E

Eliyahu Goldin

No, you can't make a simple sql query for that. You could write a stored
procedure, but it is much easier just to do it in the code with a while
loop, as you say. A good place for this is PreRender event for the control
you use for rendering the data (GridView, repeater, etc). In this event the
control is already fully built and you can loop through the Items
collection.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Exactly! But I'm having trouble writing the SQL code for this. Also,
this kind of model wouldn't work well with DataReader, since it read
one row at a time.

Would a DataSet be better here? (easier, perhaps?)


Eliyahu said:
Like this?

1 a1
a2
a3
a4
2 a1
a2
a3
a4


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


I simply want to output 4 answers for every question without repeating
the question.
(with a datareader if possible)




Eliyahu Goldin wrote:
Give an example of what you would like to see in the result record
set.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


This is the result of a JOINed query:

question answer

1 a1
1 a2
1 a3
1 a4
2 a1
2 a2
2 a3
2 a4

How would I use a DataReader to output this data without repeating
each
question 4 times?

Do I weed out the repeated question in C# with a while loop?
Or is there a way to do this in the SQL statement?

Thank you for your time and help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top