Esempio n. 1
0
        /// <summary>
        /// Generic method to fill a list from DataReader
        /// </summary>
        /// <typeparam name="T">Generic type</typeparam>
        /// <param name="reader">IDataReader</param>
        /// <returns></returns>
        public static Collection <T> FillEntities <T>(IDataReader reader) where T : class, new()
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            Collection <T> objectList = new Collection <T>();

            try
            {
                using (var objReader = new ObjectMapperCollection <T>(reader))
                {
                    foreach (T obj in objReader)
                    {
                        objectList.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                if (reader.IsClosed == false)
                {
                    reader.Close();
                }
                throw ex;
            }
            finally
            {
                if (reader.IsClosed == false)
                {
                    reader.Close();
                }
            }
            return(objectList);
        }
Esempio n. 2
0
        /// <summary>
        /// Generic method to fill an object from IDataReader
        /// </summary>
        /// <typeparam name="T">Generic type</typeparam>
        /// <param name="reader">IDataReader</param>
        /// <returns></returns>
        public static T FillEntity <T>(IDataReader reader, bool closeReader) where T : class, new()
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            ObjectMapperCollection <T> objReader = null;

            T filledObject = null;

            try
            {
                objReader = new ObjectMapperCollection <T>(reader);

                foreach (T obj in objReader)
                {
                    filledObject = obj;
                }
            }
            finally
            {
                if (reader.IsClosed == false)
                {
                    if (closeReader)
                    {
                        reader.Close();
                    }
                }
            }
            return(filledObject);
        }