public virtual List <T> SeleccionSimple(string condicion, object parameters) { string sql = string.Format("Select * From {0}", Tabla()); if (condicion != "") { sql += string.Format(" where {0}", condicion); } sql += ObtenerOrden(); using (IDbConnection db = DBConexion.Factory(ObtenerConexion())) { return(db.Query <T>(sql, parameters).ToList()); } }
public virtual Listado <T> Seleccionar(string condicion, object parameters, int pagina = 1) { string sql; string sqlOrden = ObtenerOrden(); if (ObtenerPaginacion() > 0) { sql = ObtenerSQLPaginacion(pagina, condicion); if (sqlOrden != string.Empty) { sql = sql.Replace("ORDER BY Id", sqlOrden); } } else { sql = string.Format("Select * From {0}", Tabla()); if (condicion != "") { sql += string.Format(" where {0}", condicion); } if (sqlOrden != string.Empty) { sql += $" {sqlOrden}"; } } using (IDbConnection db = DBConexion.Factory(ObtenerConexion())) { List <T> resultado = db.Query <T>(sql, parameters).ToList(); if (ObtenerPaginacion() > 0) { string sqlTotal; if (!string.IsNullOrEmpty(condicion)) { sqlTotal = string.Format("select count(1) from {0} where {1}", Tabla(), condicion); } else { sqlTotal = string.Format("select count(1) from {0}", Tabla()); } TotalRegistros = db.Query <int>(sqlTotal, parameters).Single(); } return(new Listado <T>() { TotalRegistros = this.TotalRegistros, TotalPaginas = this.TotalPaginas, Paginacion = ObtenerPaginacion(), PaginaActual = pagina, Data = resultado }); } }
public virtual void Insertar(T entidad) { string campos = string.Empty; string valores = string.Empty; string identityScope = ""; PropertyInfo Llave = ObtenerPropiedadLLave(); var propiedades = ObtenerPropiedadesInsertar(); if (Llave.PropertyType.ToString() == "System.Guid") { if (Guid.Parse(Util.Get(entidad, Llave.Name).ToString()) == Guid.Empty) { Util.Set(entidad, Llave.Name, Guid.NewGuid()); } campos += string.Format("{0}, ", Llave.Name); valores += string.Format("@{0}, ", Llave.Name); identityScope = " select 0; "; } else { identityScope = " select scope_Identity(); "; } foreach (var propiedad in ObtenerPropiedadesInsertar()) { if (propiedad.Name != Llave.Name && propiedad.CanRead && propiedad.CanWrite && EsPrimitiva(propiedad)) { campos += string.Format("{0}, ", propiedad.Name); valores += string.Format("@{0}, ", propiedad.Name); } } campos = campos.Substring(0, campos.Length - 2); valores = valores.Substring(0, valores.Length - 2); string query = string.Format("Insert into {0}({1}) values ({2}); {3}", Tabla(), campos, valores, identityScope); using (IDbConnection db = DBConexion.Factory(ObtenerConexion())) { var identity = db.Query <int>(query, entidad).Single(); if (identity > 0) { Util.Set(entidad, "Id", identity); } } }