DataReader and JOIN query

  • Thread starter Thread starter dejavue82
  • Start date Start date
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!
 
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!
 
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!
 
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!
 
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!
 
Back
Top