/// <summary> /// Metodo que recibe una entidad para almacenar los datos contenidos en ella /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="entity"></param> /// <returns>Regresa la entidad actualizada con el identificador en caso de ser autoincrementable</returns> public TEntity Guardar <TEntity>(TEntity entity) where TEntity : class, IEntity, new() { try { SqlCommand command = Connection.CreateCommand(); command.Transaction = Transaction; Statement statement = QueryZero.GetInsert(entity); foreach (KeyValuePair <string, object> entry in statement.Values) { command.Parameters.AddWithValue(entry.Key, entry.Value ?? DBNull.Value); } command.CommandText = statement.GetQueryStatement(); object idSecuencia = command.ExecuteScalar(); PropertyInfo idProperty = entity.GetType().GetProperty(statement.ColumnId); if (statement.IdentityColumn) { idProperty.SetValue(entity, Zero.Utils.ZeroUtils.ChangeType(idSecuencia, idProperty.PropertyType)); } return(entity); } catch (Exception e) { Logger.Error(Codes.ERR_00_02, e); throw new DalException(Codes.ERR_00_02, e); } }
public IList <TEntity> Consultar <TEntity>(string sql, TEntity entity, OptionsQueryZero options = null) where TEntity : class, ISelect, new() { try { SqlCommand command = Connection.CreateCommand(); command.Transaction = Transaction; Statement statement = QueryZero.GetSelect(entity, options); foreach (KeyValuePair <string, object> entry in statement.Values) { command.Parameters.AddWithValue(entry.Key, entry.Value ?? DBNull.Value); } //Se crea un data set para regresarlo DataSet theDataSet = new DataSet(); command.CommandText = sql;//se cambia por el personalizado en lugar del generado SqlDataAdapter theDataAdapter = new SqlDataAdapter(command); //Se llena e dataset theDataAdapter.Fill(theDataSet); return(Zero.Utils.ZeroUtils.DataSetToList <TEntity>(theDataSet)); } catch (Exception e) { Logger.Error(Codes.ERR_00_03, e); throw new DalException(Codes.ERR_00_01, e); } }
public IList <TEntity> ExecuteStoredProcedure <TEntity>(TEntity entity, OptionsQueryZero options = null) where TEntity : class, ISelect, new() { try { SqlCommand command = QueryZero.GetStoredProcedureCommand(entity, options); command.Connection = GetConnection(); command.Transaction = Transaction; //Se crea un data set para regresarlo DataSet theDataSet = new DataSet(); SqlDataAdapter theDataAdapter = new SqlDataAdapter(command); //Se llena e dataset theDataAdapter.Fill(theDataSet); return(Zero.Utils.ZeroUtils.DataSetToList <TEntity>(theDataSet)); } catch (Exception e) { Logger.Error(Codes.ERR_00_03, e); throw new DalException(Codes.ERR_00_16, e); } }
/// <summary> /// Metodo que recibe una entidad y hace la eliminacion del registro /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="entity"></param> public void Eliminar <TEntity>(TEntity entity) where TEntity : IEntity { try { SqlCommand command = Connection.CreateCommand(); command.Transaction = Transaction; Statement statement = QueryZero.GetDelete(entity); foreach (KeyValuePair <string, object> entry in statement.Values) { command.Parameters.AddWithValue(entry.Key, entry.Value ?? DBNull.Value); } command.CommandText = statement.GetQueryStatement(); command.ExecuteNonQuery(); } catch (Exception e) { Logger.Error(Codes.ERR_00_03, e); throw new DalException(Codes.ERR_00_03, e); } }