My problem is that this does not work;
while (reader.Read())
{
if (reader.NextResult() == true)
{
json.AppendFormat("{{\"AvgDate\": \"{0}\"}},{{\"MarkerID\": \"{1}\"}},", reader["AvgDate"], reader["MarkerID"]);
}
But this works;
while (reader.Read())
{
json.AppendFormat("{{\"AvgDate\": \"{0}\"}},{{\"MarkerID\": \"{1}\"}},", reader["AvgDate"], reader["MarkerID"]);
}
The problem with the first one is that the reader doesn’t find any data to read. I get;
“Invalid attempt to read when no data
is present.”
Can anyone see why?
NextResult() makes the reader advance to the next result set coming back from the query. The way you have it written, it would skip the first result set (likely the only one).
The pattern I think you want is:
This will check if there are any results, and if so, read the results in each result set until there are no more left to read.
EDIT: Based on comment:
For JSON, consider using a list of temporary objects, then a DataContractJsonSerializer: