Exemple #1
0
        public bool existe_en_otros_registros(string viatico_id)
        {
            bool res = false;

            UltimoMensaje = null;
            DAL.Database    db       = new Database();
            MySqlConnection conexion = Database.obtenerConexion(true);

            MySqlCommand comando = new MySqlCommand();

            comando.CommandText = "select count(*) from tareas where viatico_id=@viatico_id";
            comando.Parameters.AddWithValue("@viatico_id", viatico_id);

            comando.Transaction = Database.obtenerTransaccion();
            comando.Connection  = conexion;

            try
            {
                if (Database.obtenerTransaccion() == null)
                {
                    conexion.Open();
                }

                int registros = 0;
                int.TryParse(comando.ExecuteScalar().ToString(), out registros);
                if (registros > 0)
                {
                    res = true;
                }
            }
            catch (Exception ex)
            {
                res           = true;
                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.StackTrace = ex.StackTrace;
                UltimoMensaje.EsError    = true;
                Notify(UltimoMensaje);
            }
            finally
            {
                comando.Parameters.Clear();
                if (Database.obtenerTransaccion() == null)
                {
                    if (conexion.State != ConnectionState.Closed)
                    {
                        conexion.Close();
                    }
                }
            }

            return(res);
        }
        /// <summary>
        /// Carga en la instacia actual los atributos del código pasado por parámetro
        /// </summary>
        /// <param name="nombre">Codigo a buscar</param>
        public void obtener(string nombre)
        {
            limpiaDatos();

            MySqlConnection conexion = Database.obtenerConexion(true);

            MySqlCommand comando = new MySqlCommand(
                "SELECT * " +
                "FROM " + dbTabla + " " +
                "WHERE  " + dbCampoNumeroCompleto + " = @" + dbCampoNumeroCompleto
                , conexion);

            comando.Parameters.AddWithValue(dbCampoNumeroCompleto, nombre);
            comando.Transaction = Database.obtenerTransaccion();

            try
            {
                if (Database.obtenerTransaccion() == null)
                {
                    conexion.Open();
                }
                MySqlDataReader dr = comando.ExecuteReader();

                if (dr.Read())
                {
                    cargarDatos(this, dr);
                }
                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;
                UltimoMensaje.StackTrace = ex.StackTrace;
                Notify(UltimoMensaje);
            }
            finally
            {
                comando.Parameters.Clear();
                if (Database.obtenerTransaccion() == null)
                {
                    if (conexion.State != ConnectionState.Closed)
                    {
                        conexion.Close();
                    }
                }
            }
        }
        /// <summary>
        /// Elimina de la BD los datos correspondientes al ID de la instancia actual
        /// </summary>
        public void eliminar()
        {
            UltimoMensaje = null;
            MySqlConnection conexion = Database.obtenerConexion(true);

            MySqlCommand comando = new MySqlCommand();


            comando.CommandText = "delete from  " + dbTabla + " " +
                                  "where " + dbCampoId + " = @" + dbCampoId;
            comando.Parameters.AddWithValue(dbCampoId, Id);

            comando.Transaction = Database.obtenerTransaccion();
            comando.Connection  = conexion;

            try
            {
                if (Database.obtenerTransaccion() == null)
                {
                    conexion.Open();
                }
                comando.ExecuteNonQuery();
                limpiaDatos();
            }
            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;
                UltimoMensaje.StackTrace = ex.StackTrace;
                Notify(UltimoMensaje);
            }
            finally
            {
                comando.Parameters.Clear();
                if (Database.obtenerTransaccion() == null)
                {
                    if (conexion.State != ConnectionState.Closed)
                    {
                        conexion.Close();
                    }
                }
            }
        }
Exemple #4
0
        public object ejecutarEscalar(string consulta, MySqlConnection conexion, bool cierroConexion, params MySqlParameter[] parametros)
        {
            UltimoMensaje = null;
            MySqlCommand comando = new MySqlCommand(consulta, conexion);

            object retorno = new object();

            foreach (MySqlParameter item in parametros)
            {
                comando.Parameters.Add(item);
            }

            if (conexion.State != ConnectionState.Open)
            {
                conexion.Open();
            }


            comando.CommandTimeout = 0;


            try
            {
                retorno = comando.ExecuteScalar();
            }
            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 (conexion.State != ConnectionState.Closed && cierroConexion)
                {
                    conexion.Close();
                }
            }

            return(retorno);
        }
        /// <summary>
        /// Método encargado de conectarse con la BD para obtener los registros filtrados.
        /// </summary>
        /// <param name="itemFiltro">Items que van a utilizarse para generar la consulta (WHERE)</param>
        /// <param name="orden">Items que van a utilizarse para generar el orden (ORDER BY)</param>
        /// <param name="busquedaAnd">Indica si los terminos se conectan con AND o OR (true = AND)</param>
        /// <param name="inicio">Primer registro a mostrar</param>
        /// <param name="fin">Último registro a mostrar, o -1 para traer todos</param>
        /// <param name="totalRegistros">Total de registros que contiene el total de la consulta</param>
        /// <returns>Lista de los registros involucrados</returns>
        ///
        /// <summary>
        public List <VistaComprobantes> obtenerFiltrado(ItemFiltro[] itemFiltro,
                                                        ItemOrden[] orden, bool busquedaAnd, double inicio, double fin, out double totalRegistros)
        {
            List <VistaComprobantes> ret = new List <VistaComprobantes>();

            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 " + dbTabla + " " + 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, dbCampoNumeroCompleto);

                //TODO: Hacer Paginacion

                double rowcount = fin - (inicio - 1);

                comando.CommandText = "  SELECT *   FROM " + dbTabla + " " + where + " "
                                      + cadenaOrden
                                      + " LIMIT " + (inicio - 1) + ", " + rowcount;



                MySqlDataReader dr = comando.ExecuteReader();


                while (dr.Read())
                {
                    VistaComprobantes bar = new VistaComprobantes();
                    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);
        }