I'd like to output a string (or possibly other variable type if string isn't ideal) based on the output of a SQLDataReader read. I have been playing around with the "while" loop, while (reader.Read())
I need to end up with something I can serialize with json.net.
Here's my current code - it outputs to debug log just to make sure I was successfully reading from the database.
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "Data Source =.\\SQLEXPRESS; Initial Catalog = TeamProject; Integrated Security = True; MultipleActiveResultSets = True";
string querystring = "RosterMake";
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(querystring, connection);
command.CommandType = CommandType.StoredProcedure;
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
System.Diagnostics.Debug.WriteLine("\t{0}\t{1}\t{2}",
reader[0], reader[1], reader[2]);
// What do I put here to make the results something I can
// serialize with json.net???
}
reader.Close();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("oops");
}
}
}
}
Thanks in advance!
SqlDataAdapter.Fill aDataTable, DataTable is easy to convert to xmlSqlDataReader? Isn't it just about how to convert a string to a json object? You're showinjg a lot of code but actually you are asking for a json tutorial.SQLDataReaderstraight to json... I just don't know what the best way to store it so I can serialize it later.SqlDataReaderreads data which comes from a database and gives you an object. It has methods likereader.GetString(0)that gives you already the right type. But you have to process this string yourself, there is no automatism of the datareader. But haveyou tried this:JObject.Parse(reader.GetString(0))?