lo que quiero hacer es crear una conexion y un sentencia sql con condiciones pero me estoy haciendo una refernecia de este ejemplo.
Código PHP:
Ver original
int valor; MySqlConnection conexion = new MySqlConnection("server=localhost;user id=root; password=1234;database=proyecto"); conexion.Open(); string consulta = "SELECT intensidad as x, temperatura as y FROM sensores ORDER BY id DESC "; MySqlCommand cmd = new MySqlCommand(consulta, conexion); valor = Convert.ToInt32(cmd.ExecuteScalar()); conexion.Close(); switch (valor = 1) { case 1: consulta = "SELECT intensidad as x, temperatura as y FROM sensores ORDER BY id ASC"; cmd = new MySqlCommand(consulta, conexion); MySqlDataAdapter adpater = new MySqlDataAdapter(); DataTable dt = new DataTable(); adpater.SelectCommand = cmd; adpater.Fill(dt); string json1 = JsonConvert.SerializeObject(dt); Response.Write(json1); break; default: string sql = "SELECT intensidad as x, temperatura as y FROM sensores ORDER BY id ASC "; MySqlCommand coman = new MySqlCommand(sql, conexion); MySqlDataAdapter ad = new MySqlDataAdapter(); DataTable data = new DataTable(); ad.SelectCommand = coman; ad.Fill(data); json1 = JsonConvert.SerializeObject(data); Response.Write(json1); break; }
y este es mi codigo
Código ASP:
Ver original
int valor; MySqlConnection conexion = new MySqlConnection("server=localhost;user id=root; password=1234;database=proyecto"); conexion.Open(); string consulta = "SELECT intensidad as x, temperatura as y FROM sensores ORDER BY id DESC "; MySqlCommand cmd = new MySqlCommand(consulta, conexion); conexion.Close(); valor = Convert.ToInt32(cmd.ExecuteScalar()); switch (valor = 1) { case 1: consulta = "SELECT intensidad as x, temperatura as y FROM sensores ORDER BY id ASC"; cmd = new MySqlCommand(consulta, conexion); MySqlDataAdapter adpater = new MySqlDataAdapter(); DataTable dt = new DataTable(); adpater.SelectCommand = cmd; adpater.Fill(dt); string json1 = JsonConvert.SerializeObject(dt); Response.Write(json1); break; default: string sql = "SELECT intensidad as x, temperatura as y FROM sensores ORDER BY id ASC "; MySqlCommand coman = new MySqlCommand(sql, conexion); MySqlDataAdapter ad = new MySqlDataAdapter(); DataTable data = new DataTable(); ad.SelectCommand = coman; ad.Fill(data); json1 = JsonConvert.SerializeObject(data); Response.Write(json1); break; }