I would like to know how to convert the result of a MySQL query to JSON in C #
I do not have much knowledge in the following, but I would like the result of the query to be saved in JSON.
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetEmpleadoJSON()
{
string server = "localhost", database = "ventas", user = "root", pass = "";
MySqlConnection conectar = new MySqlConnection("server=" + server + "; database=" + database + "; Uid=" + user + "; pwd=" + pass + ";");
conectar.Open();
MySqlCommand command = conectar.CreateCommand();
//consulta select
command.CommandText = ("SELECT 'nombre' FROM 'cliente' WHERE id_cliente=901 ");
command.Connection = conectar;
MySqlDataReader reader = command.ExecuteReader();
Empleado[] emps = new Empleado[] {
new Empleado(){
Id=101,
Name=reader.ToString(),
Salary=10000
}
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(emps));
}
}