public void Update(oObjeto objeto)
    {
        // Lista de propiedades del objeto que recobe por parametro
        IList <PropertyInfo> props = new List <PropertyInfo>(objeto.GetType().GetProperties());
        // inicilizar la lista de parametros para la consutla de insercion
        List <MySqlParameter> param = new List <MySqlParameter>();

        String consulta = $"UPDATE {objeto.GetType()}  SET ";

        foreach (PropertyInfo prop in props)
        {
            if (prop.Name != "Id")
            {
                object propValue = prop.GetValue(objeto, null);
                param.Add(new MySqlParameter(prop.Name, propValue));

                consulta += $" {prop.Name} = ?{prop.Name},";
            }
        }
        // cada vez que finalize hacemos un substring de la ultima coma para cerrar la consulta

        consulta = "SET FOREIGN_KEY_CHECKS = 0; " + consulta.Substring(0, consulta.Length - 1) + " WHERE Id = " + objeto.Id;


        Conexion.ConsultaParametros(consulta, param);
    }
    public ObjetoConexion(oObjeto t)
    {
        this.tipo     = t;
        this.Conexion = new Conexion();
        string consulta = $"SELECT COUNT(*) as 'cantidad' FROM {tipo.GetType()} ";
        var    ds       = Conexion.consultaDataTable(consulta);

        if (ds.Tables[0].Rows.Count > 0)
        {
            this.cantidadRegistros = Convert.ToInt32(ds.Tables[0].Rows[0].ItemArray[0]);
        }
    }
    public int Insert(oObjeto objeto)
    {
        // Lista de propiedades del objeto que recobe por parametro
        IList <PropertyInfo> props = new List <PropertyInfo>(objeto.GetType().GetProperties());
        // inicilizar la lista de parametros para la consutla de insercion
        List <MySqlParameter> param = new List <MySqlParameter>();
        //cadenas de consulta
        String consulta = $"INSERT INTO {objeto.GetType()} ( ";
        String valores  = " VALUES (";

        // recorre las propiedades del objeto
        foreach (PropertyInfo prop in props)
        {
            if (prop.Name != "Id")
            {
                object propValue = prop.GetValue(objeto, null);
                param.Add(new MySqlParameter(prop.Name, propValue));

                consulta += $" {prop.Name} ,";
                valores  += $" ?{prop.Name} ,";
                // Do something with propValue
            }
        }
        // cada vez que finalize hacemos un substring de la ultima coma para cerrar la consulta
        consulta = consulta.Substring(0, consulta.Length - 1) + " )";
        valores  = valores.Substring(0, valores.Length - 1) + " )";

        // Unimos consulta y valores

        consulta = consulta + valores;


        Conexion.ConsultaParametros(consulta, param);


        consulta = $"SELECT IFNULL(max(id),0) as id FROM {this.tipo.GetType()}";

        var ds = Conexion.consultaDataTable(consulta);
        var id = 0;

        if (ds != null)
        {
            id = Convert.ToInt32(ds.Tables[0].Rows[0].ItemArray[0]);
        }

        return(id);
    }
    public bool Delete(int Id, oObjeto param = null, string[] filtros = null)
    {
        //si el objeto no es null se elimina ese objeto
        if (filtros != null)
        {
            String where = $"WHERE 1 ";
            List <MySqlParameter> parametro = new List <MySqlParameter>();
            for (var f = 0; f < filtros.Length; f++)
            {
                if (f % 2 == 0)
                {
                    where += $"AND {filtros[f]} = ?{filtros[f]} ";
                    parametro.Add(new MySqlParameter(filtros[f], filtros[f + 1]));
                }
            }

            String consulta = $"DELETE FROM {this.tipo.GetType()} " + where;


            return(Conexion.ConsultaParametros(consulta, parametro));
        }
        else if (param != null)
        {
            String consulta = $"DELETE FROM {this.tipo.GetType()} WHERE Id = ?Id";
            List <MySqlParameter> parametro = new List <MySqlParameter>();
            parametro.Add(new MySqlParameter("Id", param.Id));
            return(Conexion.ConsultaParametros(consulta, parametro));
        }
        else
        {
            //si el objeto es nulo se elimina por ID
            String consulta = $"DELETE FROM {this.tipo.GetType()} WHERE Id = ?Id";
            List <MySqlParameter> parametro = new List <MySqlParameter>();
            parametro.Add(new MySqlParameter("Id", Id));
            return(Conexion.ConsultaParametros(consulta, parametro));
        }
        return(false);
    }
Example #5
0
 private CarreraConexion(oObjeto aux) : base(aux)
 {
 }
 private DocenteConexion(oObjeto aux) : base(aux)
 {
 }