ورود

View Full Version : سوال: دسترسی به فیلدهای sql data reader در Json



mahdidabaghi
شنبه 07 بهمن 1391, 00:34 صبح
سلام به دوستان
لطفا منو راهنمایی کنید چطور میتونم رکوردی رو که با sql data reader توی web service در Asp خوندم رو به تک تک فیلدهای اون در Json دسترسی داشته باشم
با تشکر

pouyan.hosseini
یک شنبه 08 بهمن 1391, 21:21 عصر
// ... SQL connection and command set up, only querying 1 row from the table
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonWriter jsonWriter = new JsonTextWriter(sw);

try {

theSqlConnection.Open(); // open the connection

// read the row from the table
SqlDataReader reader = sqlCommand.ExecuteReader();
reader.Read();

int fieldcount = reader.FieldCount; // count how many columns are in the row
object[] values = new object[fieldcount]; // storage for column values
reader.GetValues(values); // extract the values in each column

jsonWriter.WriteStartObject();
for (int index = 0; index < fieldcount; index++) { // iterate through all columns

jsonWriter.WritePropertyName(reader.GetName(index) ); // column name
jsonWriter.WriteValue(values[index]); // value in column

}
jsonWriter.WriteEndObject();

reader.Close();

} catch (SqlException sqlException) { // exception
context.Response.ContentType = "text/plain";
context.Response.Write("Connection Exception: ");
context.Response.Write(sqlException.ToString() + "\n");
} finally {
theSqlConnection.Close(); // close the connection
}
// END of method
// the above method returns sb and another uses it to return as HTTP Response...
StringBuilder theTicket = getInfo(context, ticketID);
context.Response.ContentType = "application/json";
context.Response.Write(theTicket);

$.ajax({
type: 'GET',
url: 'Preview.ashx',
data: 'ticketID=' + ticketID,
dataType: "json",
success: function (data) {

// data is the JSON object the server spits out
// do stuff with the data
}
});