예제 #1
0
        public IEnumerable <IMessage> GetMessagesBetweenDate(DateTime from, DateTime to)
        {
            var parameters = new List <NpgsqlParameter>();

            parameters.Add(_postgreHelper.CreateParameter("@from", from, DbType.DateTime));
            parameters.Add(_postgreHelper.CreateParameter("@to", to, DbType.DateTime));

            var sqlCommand = "SELECT content, dateTime, number FROM messages WHERE dateTime BETWEEN @from AND @to";

            var dataReader = _postgreHelper.GetDataReader(sqlCommand, CommandType.Text, parameters, out _connection);

            try
            {
                var messages = new List <IMessage>();
                while (dataReader.Read())
                {
                    var message = new Message();
                    message.Content  = dataReader["content"].ToString();
                    message.DateTime = dataReader.GetDateTime("dateTime");
                    message.Number   = dataReader.GetInt32("number");

                    messages.Add(message);
                }
                return(messages);
            }
            catch (NpgsqlException ex)
            {
                _logger.LogError(ex.Message);
                throw ex;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                throw ex;
            }
            finally
            {
                dataReader.Close();
                CloseConnection();
            }
        }