public static int Agregar(MySqlConnection con, DAOComprar Comprar) { int retorno = 0; MySqlCommand comando = new MySqlCommand(string.Format("INSERT INTO Compras (id_compra,proveedor,cantidad,producto,fecha_compra,total) values ('{0}','{1}','{2}','{3}','{4}','{5}')", Comprar.id, Comprar.proveedor, Comprar.cantidad, Comprar.producto, Comprar.fecha_compra, Comprar.total), con); retorno = comando.ExecuteNonQuery(); MySqlCommand comando2 = new MySqlCommand(string.Format("INSERT INTO movimientos (id_movimiento,nombre_movimiento,fecha_movimiento,cantidad,cliente,proveedor,total) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", Comprar.id, "compra", Comprar.fecha_compra, Comprar.cantidad, "sin cliente", Comprar.proveedor, Comprar.total), con); retorno = comando2.ExecuteNonQuery(); MySqlCommand comando3 = new MySqlCommand(string.Format("update productos set cantidad=cantidad+'{0}' where nombre='{1}'", Comprar.cantidad, Comprar.producto), con); retorno = comando3.ExecuteNonQuery(); return(retorno); }
private void btn_comprar_Click(object sender, EventArgs e) { if (precio_comp.id != int.Parse(dgvComprar.CurrentRow.Cells[0].Value.ToString())) { MessageBox.Show("Calcule el total del nuevo producto"); } else if (txt_Cantidad.Text == "0" || txt_Cantidad.Text == "") { MessageBox.Show("Ingrese una cantidad valida.."); } else { if (MessageBox.Show("Seguro que desea Agregar?", "Confirmación Agregar", MessageBoxButtons.YesNo) == DialogResult.Yes) { try { if (con.Abrirconexion() == true) { DAOComprar Comprar = new DAOComprar(); Comprar.proveedor = dgvComprar.CurrentRow.Cells[5].Value.ToString(); Comprar.producto = dgvComprar.CurrentRow.Cells[1].Value.ToString(); Comprar.cantidad = int.Parse(txt_Cantidad.Text); DateTime localDate = DateTime.Now; Comprar.fecha_compra = localDate.ToString(); Comprar.total = total; int resultado = DAOComprar.Agregar(con.con, Comprar); if (resultado > 0) { txt_Cantidad.Clear(); lbl_total.Text = "______"; MessageBox.Show("Compra Almacenada!"); txt_Cantidad.Text = "1"; } } } catch (MySql.Data.MySqlClient.MySqlException ex) { MessageBox.Show(ex.Message); } con.Cerrarconexion(); } } }
public static IList <DAOComprar> Buscar(MySqlConnection con, string Nombre) { List <DAOComprar> lista = new List <DAOComprar>(); MySqlCommand comando = new MySqlCommand(string.Format("SELECT productos.nombre,proveedores.nombre from productos,proveedores where productos.nombre LIKE ('%{0}%') and productos.id_proveedor=proveedores.id_proveedor", Nombre), con); MySqlDataReader reader = comando.ExecuteReader(); while (reader.Read()) { DAOComprar comprar = new DAOComprar(); comprar.producto = reader.GetString(0); comprar.proveedor = reader.GetString(1); lista.Add(comprar); } return(lista); }