public object Buscar(params object[] filters) { string where = ""; int codCotizacion; if (filters.Length != 0) { codCotizacion = Convert.ToInt32(filters[0]); where += "idCotizacion = " + codCotizacion; } DBConexion DB = new DBConexion(); SqlConnection conn = DB.conn; SqlCommand cmd = DB.cmd; SqlDataReader reader; cmd.CommandText = "SELECT * FROM CotizacionxProducto WHERE " + where; cmd.CommandType = CommandType.Text; cmd.Connection = conn; List<CotizacionxProducto> lstCotizaciones = new List<CotizacionxProducto>(); try { conn.Open(); reader = cmd.ExecuteReader(); while (reader.Read()) { CotizacionxProducto c = new CotizacionxProducto(); c.IdCotizacion = Convert.ToInt32(reader["idCotizacion"].ToString()); c.Producto = new ProductoSQL().Buscar_por_CodigoProducto(Convert.ToInt32(reader["idProducto"].ToString())); c.Cantidad = Convert.ToInt32(reader["cantidad"].ToString()); c.Precio = Convert.ToDouble(reader["precio"].ToString()); lstCotizaciones.Add(c); } conn.Close(); } catch (Exception e) { MessageBox.Show(e.StackTrace.ToString()); } return lstCotizaciones; }
//Funciones para importar desde un Excel public void Cargar() { if (path != "") { List<CotizacionxProducto> lista = new List<CotizacionxProducto>(); String name = "Cotizaciones"; String constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False"; OleDbConnection con = new OleDbConnection(constr); OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con); con.Open(); OleDbDataAdapter sda = new OleDbDataAdapter(oconn); DataTable data = new DataTable(); sda.Fill(data); DataTableReader ds = data.CreateDataReader(); while (ds.Read()) { CotizacionxProducto cp = new CotizacionxProducto(); cp.Producto = new Producto(); cp.Producto.CodigoProd = ds["Codigo"].ToString(); cp.Producto.Nombre = ds["Descripcion"].ToString(); cp.Cantidad = Convert.ToInt32(ds["Cantidad"].ToString()); cp.Precio = Convert.ToDouble(ds["Precio"].ToString()); lista.Add(cp); } LstProducto = lista; } }
public int InsertarValidado(CotizacionxProducto cp) { Producto p = new ProductoSQL().Buscar_por_CodigoProducto(cp.Producto.CodigoProd); int k = 0; cp.Producto = p; if (p != null) { DBConexion DB = new DBConexion(); SqlConnection conn = DB.conn; SqlCommand cmd = DB.cmd; //MessageBox.Show("IDcot = " + cp.IdCotizacion + " IDprod = " + cp.Producto.IdProducto + " precio = " + cp.Precio + " cantidad = " + cp.Cantidad); cmd.CommandText = "IF NOT EXISTS(SELECT 1 from CotizacionxProducto where idCotizacion = @idCotizacion and idProducto = @idProducto) " + "Insert into CotizacionxProducto(idCotizacion,idProducto,cantidad,precio) " + "VALUES (@idCotizacion,@idProducto,@cantidad,@precio) " + " else " + "UPDATE CotizacionxProducto set cantidad = @cantidad, precio = @precio " + "where idCotizacion = @idCotizacion and idProducto = @idProducto "; cmd.Parameters.AddWithValue("@idCotizacion", cp.IdCotizacion); cmd.Parameters.AddWithValue("@idProducto", cp.Producto.IdProducto); cmd.Parameters.AddWithValue("@cantidad", cp.Cantidad); cmd.Parameters.AddWithValue("@precio", cp.Precio); cmd.CommandType = CommandType.Text; cmd.Connection = conn; try { conn.Open(); k = cmd.ExecuteNonQuery(); conn.Close(); } catch (SqlException e) { // MessageBox.Show(e.StackTrace.ToString()); } } return k; }