public Carrito obtenerUno(int idProducto) { ///CREAR CONEXION, MODIFICARLA, USARLA MySqlConnection cn = new MySqlConnection(); try { daoprod = new daoProducto(); Carrito carrito = null; cn.ConnectionString = conexion; cn.Open(); ///EJECUTAR COMANDO string strSQL = "SELECT * FROM CARRITO WHERE ID_PRODUCTO = @ID"; MySqlCommand comando = new MySqlCommand(strSQL, cn); comando.Parameters.AddWithValue("ID", idProducto); MySqlDataReader dr = comando.ExecuteReader(); if (dr.Read()) { carrito = new Carrito(); carrito.idCliente = dr.GetString("ID_CLIENTE"); carrito.idProducto = dr.GetInt32("ID_PRODUCTO"); carrito.nombreProducto = daoprod.obtenerUno(dr.GetInt32("ID_PRODUCTO")).nombre; carrito.cantidad = dr.GetInt32("CANTIDAD"); carrito.subtotal = (daoprod.obtenerUno(dr.GetInt32("ID_PRODUCTO")).precio) * dr.GetInt32("CANTIDAD"); } comando.Dispose(); return(carrito); } catch (Exception e) { throw e; } finally { ///CERRAR CONEXION cn.Close(); cn.Dispose(); } }
public List <Carrito> obtenerTodos(String idCliente) { ///CREAR CONEXION, MODIFICARLA, USARLA List <Carrito> elcarrito = new List <Carrito>(); MySqlConnection cn = new MySqlConnection(); daoprod = new daoProducto(); try { cn.ConnectionString = conexion; cn.Open(); ///EJECUTAR COMANDO string strSQL = "SELECT * FROM CARRITO WHERE ID_CLIENTE=@ID_CLIENTE"; MySqlCommand comando = new MySqlCommand(strSQL, cn); comando.Parameters.AddWithValue("ID_CLIENTE", idCliente); MySqlDataReader dr = comando.ExecuteReader(); while (dr.Read()) { Carrito car = new Carrito ( dr.GetString("ID_CLIENTE"), dr.GetInt32("ID_PRODUCTO"), daoprod.obtenerUno(dr.GetInt32("ID_PRODUCTO")).nombre, dr.GetInt32("CANTIDAD"), dr.GetDouble("SUBTOTAL") ); elcarrito.Add(car); } comando.Dispose(); return(elcarrito); } catch (Exception e) { throw e; } finally { ///CERRAR CONEXION cn.Close(); cn.Dispose(); } }