Exemplo n.º 1
0
        // GetDataPage
        public DataPage <T> GetDataPage <T>(SqlCommand command, int pageIndex, int pageSize) where T : class
        {
            SqlDataReader reader = null;
            DataPage <T>  page   = new DataPage <T>();

            page.PageIndex = pageIndex;
            page.PageSize  = pageSize;
            try
            {
                if (command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    IDataMapper mapper = new DataMapperFactory().GetMapper(typeof(T));
                    while (reader.Read())
                    {
                        // get the data for this row
                        T dto = null;
                        dto = (T)mapper.GetData(reader);
                        page.Data.Add(dto);
                        // If we haven't set the RecordCount yet then set it
                        if (page.RecordCount == 0)
                        {
                            page.RecordCount = mapper.GetRecordCount(reader);
                        }
                    }
                    reader.Close();
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error populating data", e);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (_trans == null)
                {
                    command.Connection.Close();
                }
            }
            return(page);
        }