public static string add_Producto(Producto d) { return "{" + '"' + "IdProducto" + '"' + ": " + d.IdProducto.ToString() + ',' + '"' + "IdTipoProducto" + '"' + ": " + d.IdTipoProducto.ToString() + ',' + '"' + "Nombre" + '"' + ": " + '"' + d.Nombre + '"' + ',' + '"' + "Descripcion" + '"' + ": " + '"' + d.Descripcion + '"' + ',' + '"' + "IdUnidadProducto" + '"' + ": " + d.IdUnidadProducto.ToString() + ',' + '"' + "IdMoneda" + '"' + ": " + d.IdMoneda.ToString() + ',' + '"' + "Presentacion" + '"' + ": " + d.Presentacion.ToString() + ',' + '"' + "Perecible" + '"' + ": " + (d.Perecible ? "true" : "false") + ',' + '"' + "Activo" + '"' + ": " + (d.Activo ? "true" : "false") + ',' + '"' + "PrecioUnitario" + '"' + ": " + d.PrecioUnitario.ToString() + ',' + '"' + "Observaciones" + '"' + ": " + '"' + d.Observaciones + '"' + ',' + '"' + "FechaCreacion" + '"' + ": " + '"' + Utils.dateToJson(d.FechaCreacion) + '"' + ',' + '"' + "Imagen" + '"' + ": " + '"' + d.Imagen + '"' + "}"; }
public Producto selectById_Producto(int id) { try { Producto p = new Producto(); DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(); string ConnString = ConfigurationManager.ConnectionStrings["barabaresConnectionString"].ConnectionString; using (SqlConnection SqlConn = new SqlConnection(ConnString)) { try { SqlConn.Open(); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); return p; } SqlCommand sqlCmd = new SqlCommand("PRODUCTO_SELECT_BY_ID", SqlConn); sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.Parameters.Add("@ipnIdProducto", SqlDbType.Int).Value = id; sda.SelectCommand = sqlCmd; sda.Fill(dt); SqlConn.Close(); sqlCmd.Dispose(); sda.Dispose(); } DataRow[] rows = dt.Select(); for (int i = 0; i < rows.Length; i++) { p = Utils.producto_parse(rows[i]); } return p; } catch (Exception ex) { Producto prd = new Producto(); LogBarabares b = new LogBarabares() { Accion = Constantes.LOG_LISTAR, Servicio = Constantes.SelectById_Producto, Input = JsonSerializer.selectById(id), Descripcion = ex.ToString(), Clase = prd.GetType().Name, Aplicacion = Constantes.ENTORNO_SERVICIOS, Estado = Constantes.FALLA, Ip = "", IdUsuario = 1 //TODO: obtener usuario de la sesión }; Utils.add_LogBarabares(b); return prd; } }
public static Producto producto_parse(DataRow r) { Producto p = new Producto(); p.IdProducto = Int32.Parse(r["idProducto"].ToString()); p.Nombre = r["nombre"].ToString(); p.Descripcion = r["descripcion"].ToString(); p.Perecible = Boolean.Parse(r["perecible"].ToString()); p.PrecioUnitario = Double.Parse(r["precioUnitario"].ToString()); p.FechaCreacion = DateTime.ParseExact(r["fechaCreacion"].ToString(), "M/d/yyyy h:mm:ss ttt", null); p.UltimaModificacion = DateTime.ParseExact(r["ultimaModificacion"].ToString(), "M/d/yyyy h:mm:ss ttt", null); p.IdTipoProducto = Int32.Parse(r["idTipoProducto"].ToString()); p.IdUnidadProducto = Int32.Parse(r["idUnidadProducto"].ToString()); p.Imagen = r["imagen"].ToString(); p.Presentacion = Int32.Parse(r["presentacion"].ToString()); p.Observaciones = r["observaciones"].ToString(); p.Activo = Boolean.Parse(r["activo"].ToString()); return p; }
public ResponseBD add_Producto(Producto p) { try { ResponseBD response = new ResponseBD(); string ConnString = ConfigurationManager.ConnectionStrings["barabaresConnectionString"].ConnectionString; using (SqlConnection SqlConn = new SqlConnection(ConnString)) { try { SqlConn.Open(); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); response.Flujo = Constantes.FALLA; response.Mensaje = "Error al abrir la conexión a BD"; return response; } SqlCommand sqlCmd = new SqlCommand("PRODUCTO_INSERT", SqlConn); sqlCmd.CommandType = CommandType.StoredProcedure; SqlParameter flujo = new SqlParameter("@opsFlujo", SqlDbType.VarChar) { Direction = ParameterDirection.Output, Size = 10 }; SqlParameter mensaje = new SqlParameter("@opsMsj", SqlDbType.VarChar) { Direction = ParameterDirection.Output, Size = 100 }; sqlCmd.Parameters.Add("@ipsNombre", SqlDbType.VarChar).Value = p.Nombre; sqlCmd.Parameters.Add("@ipsDescripcion", SqlDbType.VarChar).Value = p.Descripcion; sqlCmd.Parameters.Add("@ipbPerecible", SqlDbType.Bit).Value = p.Perecible; sqlCmd.Parameters.Add("@ipnPrecioUnitario", SqlDbType.Real).Value = p.PrecioUnitario; sqlCmd.Parameters.Add("@ipdFechaCreacion", SqlDbType.DateTime).Value = p.FechaCreacion; sqlCmd.Parameters.Add("@ipnIdTipoProducto", SqlDbType.Int).Value = p.IdTipoProducto; sqlCmd.Parameters.Add("@ipnIdUnidadProducto", SqlDbType.Int).Value = p.IdUnidadProducto; sqlCmd.Parameters.Add("@ipnIdMoneda", SqlDbType.Int).Value = p.IdMoneda; sqlCmd.Parameters.Add("@ipsImagen", SqlDbType.VarChar).Value = p.Imagen; sqlCmd.Parameters.Add("@ipnPresentacion", SqlDbType.Int).Value = p.Presentacion; sqlCmd.Parameters.Add("@ipsObservaciones", SqlDbType.VarChar).Value = p.Observaciones; sqlCmd.Parameters.Add("@ipbActivo", SqlDbType.VarChar).Value = p.Activo; sqlCmd.Parameters.Add(flujo); sqlCmd.Parameters.Add(mensaje); sqlCmd.ExecuteNonQuery(); response.Flujo = flujo.Value.ToString(); response.Mensaje = mensaje.Value.ToString(); SqlConn.Close(); } return response; } catch (Exception ex) { LogBarabares b = new LogBarabares() { Accion = Constantes.LOG_CREAR, Servicio = Constantes.Add_Producto, Input = JsonSerializer.add_Producto(p), Descripcion = ex.ToString(), Clase = (p == null) ? "null" : p.GetType().Name, Aplicacion = Constantes.ENTORNO_SERVICIOS, Estado = Constantes.FALLA, Ip = "", IdUsuario = 1 //TODO: obtener usuario de la sesión }; Utils.add_LogBarabares(b); ResponseBD response = new ResponseBD(); response.Flujo = Constantes.FALLA; response.Mensaje = "Error al abrir la conexión a BD"; return response; } }