/// <summary> /// maps object / entity with data /// </summary> /// <param name="reader"></param> public IList <T> MapWithoutAttributes <T>(SQLDatabaseResultSet reader) where T : class, new() { IList <T> collection = new List <T>(); for (int r = 0; r < reader.RowCount; r++) { T obj = new T(); int c = 0; foreach (PropertyInfo i in obj.GetType().GetProperties().ToList()) { try { if (ColumnValue(reader, r, i.Name) != DBNull.Value) { i.SetValue(obj, ColumnValue(reader, r, i.Name)); } else { i.SetValue(obj, null); } } catch (Exception ex) { throw ex; } c++; } collection.Add(obj); } return(collection); }
/// <summary> /// returns single value of specified row index and column name /// </summary> /// <param name="rs"></param> /// <param name="RowIndex"></param> /// <param name="ColumnName"></param> public object ColumnValue(SQLDatabaseResultSet rs, int RowIndex, string ColumnName) { if ((RowIndex > -1) && (!string.IsNullOrWhiteSpace(ColumnName))) { if (RowIndex > rs.RowCount) { throw new Exception("Row index is out of range"); } int ColIndex = -1; foreach (string col in rs.Columns) { ColIndex++; if (col.Equals(ColumnName, StringComparison.CurrentCultureIgnoreCase)) { break; } } if ((ColIndex == -1) || (ColIndex > rs.ColumnCount)) { throw new Exception(string.Format("Column {0} not found.", ColumnName)); } return(rs.Rows[RowIndex][ColIndex]); } else { throw new Exception("Column name and valid row index are required."); } }
/// <summary> /// maps object / entity with data /// </summary> /// <param name="reader"></param> public IList <T> Map <T>(SQLDatabaseResultSet reader) where T : class, new() { IList <T> collection = new List <T>(); for (int r = 0; r < reader.RowCount; r++) { T obj = new T(); int c = 0; foreach (PropertyInfo i in obj.GetType().GetProperties() .Where(p => p.CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(DBColumnAttribute)) != null).ToList()) { try { var ca = i.GetCustomAttribute(typeof(DBColumnAttribute)); if (ca != null) { try { if (ColumnValue(reader, r, i.Name) != DBNull.Value) { i.SetValue(obj, ColumnValue(reader, r, i.Name)); } } catch (ArgumentException) // Argument Exception occurs when data types don't match. { // Try changing type of other wise next Exception ex will still catch the exception. i.SetValue(obj, Convert.ChangeType(ColumnValue(reader, r, i.Name), i.PropertyType)); } } } catch (Exception ex) { throw ex; } c++; } collection.Add(obj); } return(collection); }
/// <summary> /// returns single value of specified row and column index /// </summary> /// <param name="rs"></param> /// <param name="RowIndex"></param> /// <param name="ColumnIndex"></param> public object ColumnValue(SQLDatabaseResultSet rs, int RowIndex, int ColumnIndex) { if ((RowIndex > -1) && (ColumnIndex > -1)) { if (RowIndex > rs.RowCount) { throw new Exception("Row index is out of range"); } if (ColumnIndex > rs.ColumnCount) { throw new Exception("Column index is out of range"); } return(rs.Rows[RowIndex][ColumnIndex]); } else { throw new Exception("Invalid Row or Column index"); } }
private IList <TEntity> ExecuteGet <TEntity>(SQLDatabaseResultSet reader) where TEntity : class, new() { return(new EntityMapper().Map <TEntity>(reader)); }