KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
UPDATE I figured it out. Check out my answer below . I'm trying to create a JSON string representing a row from a database table to return in an HTTP response. It seems like Json.NET would be a good tool to utilize. However, I'm not sure how to do build the JSON string while I'm reading from the database. The problem is marked by the obnoxious comments /******** ********/ // connect to DB theSqlConnection.Open(); // open the connection SqlDataReader reader = sqlCommand.ExecuteReader(); if (reader.HasRows) { while(reader.Read()) { StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); using (JsonWriter jsonWriter = new JsonTextWriter(sw)) { // read columns from the current row and build this JsonWriter jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("FirstName"); // I need to read the value from the database /******** I can't just say reader[i] to get the ith column. How would I loop here to get all columns? ********/ jsonWriter.WriteValue(... ? ...); jsonWriter.WritePropertyName("LastName"); jsonWriter.WriteValue(... ? ...); jsonWriter.WritePropertyName("Email"); jsonWriter.WriteValue(... ? ...); // etc... jsonWriter.WriteEndObject(); } } } The problem is that I don't know how to read each column from the row from the SqlReader such that I can call WriteValue and give it the correct information and attach it to the correct column name. So if a row looks like this... | FirstName | LastName | Email | ... how would I c
Tags (comma-separated)
Save Edits
Cancel