public bool guardar(bool nuevo) { bool ret = false; Tipos_Gastos bar = new Tipos_Gastos(); MySqlConnection conexion = Database.obtenerConexion(true); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conexion; cmd.Transaction = Database.obtenerTransaccion(); if (nuevo) { cmd.CommandText = "insert into tipos_gastos (detalles) values (@detalles)"; } else { cmd.CommandText = "update tipos_gastos set detalles=@detalles where id=@id"; } cmd.Parameters.AddWithValue("@id", id); cmd.Parameters.AddWithValue("@detalles", detalles); try { if (Database.obtenerTransaccion() == null) { conexion.Open(); } cmd.ExecuteNonQuery(); } catch (Exception ex) { UltimoMensaje = GestionErrores.obtenerError(ex); UltimoMensaje.cargar( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), new System.Diagnostics.StackFrame(0, true).GetFileLineNumber()); UltimoMensaje.EsError = true; Notify(UltimoMensaje); } finally { cmd.Parameters.Clear(); if (Database.obtenerTransaccion() == null) { if (conexion.State != ConnectionState.Closed) { conexion.Close(); } } } return(ret); }
public Tipos_Gastos obtener(string id) { Tipos_Gastos bar = new Tipos_Gastos(); MySqlConnection conexion = Database.obtenerConexion(true); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conexion; cmd.Transaction = Database.obtenerTransaccion(); cmd.CommandText = "select * from tipos_gastos where id = @filtro"; cmd.Parameters.AddWithValue("@filtro", id); try { if (Database.obtenerTransaccion() == null) { conexion.Open(); } MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { bar.id = Convert.ToInt32(reader["id"].ToString()); bar.detalles = reader["detalles"].ToString(); } reader.Close(); } catch (Exception ex) { UltimoMensaje = GestionErrores.obtenerError(ex); UltimoMensaje.cargar( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), new System.Diagnostics.StackFrame(0, true).GetFileLineNumber()); UltimoMensaje.EsError = true; Notify(UltimoMensaje); } finally { cmd.Parameters.Clear(); if (Database.obtenerTransaccion() == null) { if (conexion.State != ConnectionState.Closed) { conexion.Close(); } } } return(bar); }
public void borrar(string id) { Tipos_Gastos bar = new Tipos_Gastos(); MySqlConnection conexion = Database.obtenerConexion(true); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conexion; cmd.Transaction = Database.obtenerTransaccion(); cmd.CommandText = "delete from tipos_gastos where id=@id"; cmd.Parameters.AddWithValue("@id", id); try { if (Database.obtenerTransaccion() == null) { conexion.Open(); } cmd.ExecuteNonQuery(); } catch (Exception ex) { UltimoMensaje = GestionErrores.obtenerError(ex); UltimoMensaje.cargar( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), new System.Diagnostics.StackFrame(0, true).GetFileLineNumber()); UltimoMensaje.EsError = true; Notify(UltimoMensaje); } finally { cmd.Parameters.Clear(); if (Database.obtenerTransaccion() == null) { if (conexion.State != ConnectionState.Closed) { conexion.Close(); } } } }
} // Unsubscribe #endregion public List <Tipos_Gastos> obtenerFiltrado(ItemFiltro[] itemFiltro, ItemOrden[] orden, bool busquedaAnd, double inicio, double fin, out double totalRegistros) { List <Tipos_Gastos> ret = new List <Tipos_Gastos>(); UltimoMensaje = null; MySqlConnection conexion = Database.obtenerConexion(true); MySqlCommand comando = new MySqlCommand(); comando.Connection = conexion; comando.Transaction = Database.obtenerTransaccion(); totalRegistros = 0; int parameterCount = 0; string where = ""; string tipoBusqueda = " AND "; if (!busquedaAnd) { tipoBusqueda = " OR "; } Varios.armarConsultaFiltros(itemFiltro, comando, ref parameterCount, ref where, tipoBusqueda); string cadenaOrden = ""; comando.CommandText = "SELECT count(*) FROM tipos_gastos " + where; try { if (Database.obtenerTransaccion() == null) { conexion.Open(); } double.TryParse(comando.ExecuteScalar().ToString(), out totalRegistros); if (inicio < 0) { inicio = 0; } if (inicio > totalRegistros) { inicio = totalRegistros - 1; } if (fin > totalRegistros || fin == -1) { fin = totalRegistros; } if (inicio < 1) { inicio = 1; } if (fin < 1) { fin = 1; } cadenaOrden = Varios.armarCadenaOrden(orden, cadenaOrden, "detalles"); //TODO: Hacer Paginacion double rowcount = fin - (inicio - 1); comando.CommandText = " SELECT * FROM tipos_gastos " + where + " " + cadenaOrden + " LIMIT " + (inicio - 1) + ", " + rowcount; MySqlDataReader dr = comando.ExecuteReader(); while (dr.Read()) { Tipos_Gastos bar = new Tipos_Gastos(); bar.Subscribe(this); cargarDatos(bar, dr); ret.Add(bar); } dr.Close(); } catch (Exception ex) { UltimoMensaje = GestionErrores.obtenerError(ex); UltimoMensaje.cargar( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString(), System.Reflection.MethodBase.GetCurrentMethod().ToString(), new System.Diagnostics.StackFrame(0, true).GetFileLineNumber()); UltimoMensaje.EsError = true; Notify(UltimoMensaje); } finally { comando.Parameters.Clear(); if (Database.obtenerTransaccion() == null) { if (conexion.State != ConnectionState.Closed) { conexion.Close(); } } } return(ret); }
private static void cargarDatos(Tipos_Gastos objeto, MySqlDataReader dr) { objeto.id = Convert.ToInt32(dr["id"].ToString()); objeto.detalles = dr["detalles"].ToString(); }