Exemplo n.º 1
0
        // GetSingleDTO
        protected static T GetSingleDTO <T>(ref SqlCommand command) where T : DTOBase
        {
            T dto = null;

            try
            {
                command.Connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    reader.Read();
                    DTOParserSQLClient parser = DTOParserFactory.GetParserSQLClient(typeof(T));
                    parser.PopulateOrdinals(reader);
                    dto = (T)parser.PopulateDTO(reader);
                    reader.Close();
                }
                else
                {
                    // Whever there's no data, we return null.
                    dto = null;
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error populating data", e);
            }
            finally
            {
                command.Connection.Close();
                command.Connection.Dispose();
            }
            // return the DTO, it's either populated with data or null.
            return(dto);
        }
Exemplo n.º 2
0
        // GetDTOList
        protected static List <T> GetDTOList <T>(ref SqlCommand command) where T : DTOBase
        {
            List <T> dtoList = new List <T>();

            try
            {
                command.Connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    // Get a parser for this DTO type and populate
                    // the ordinals.
                    DTOParserSQLClient parser = DTOParserFactory.GetParserSQLClient(typeof(T));
                    parser.PopulateOrdinals(reader);
                    // Use the parser to build our list of DTOs.
                    while (reader.Read())
                    {
                        T dto = null;
                        dto = (T)parser.PopulateDTO(reader);
                        dtoList.Add(dto);
                    }
                    reader.Close();
                }
                else
                {
                    // Whenver there's no data, we return null.
                    //dtoList = null;
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error populating data", e);
            }
            finally
            {
                command.Connection.Close();
                command.Connection.Dispose();
            }
            return(dtoList);
        }