Ejemplo n.º 1
0
        public static List <Question> GetAllQuestionsWithoutAnswers(DBConnection connection)
        {
            SqlCommand command = connection.GetCommand(QuestionTable.SQL_SELECT_ALL_QUESTIONS_NO_ANSWERS);

            using (SqlDataReader reader = command.ExecuteReader())
            {
                List <Question> questions = new List <Question>();

                while (reader.Read())
                {
                    questions.Add(QuestionTable.ReadQuestion(reader));
                }

                return(questions);
            }
        }
Ejemplo n.º 2
0
        public static Question GetQuestionById(DBConnection connection, int questionId)
        {
            SqlCommand command = connection.GetCommand(QuestionTable.SQL_SELECT_QUESTION_BY_ID);

            command.AddParameter("@questionId", SqlDbType.Int, questionId);

            try
            {
                Question question = null;

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (question == null)
                        {
                            question = QuestionTable.ReadQuestion(reader);
                        }

                        QuestionAnswer answer = new QuestionAnswer(question.Id, reader.GetColumnValue <int>("index"), reader.GetColumnValue <string>("qa_text"), reader.GetColumnValue <bool>("correct"));
                        question.AddAnswer(answer);
                    }
                }

                if (question == null)
                {
                    throw new DatabaseException("Question does not exist");
                }

                return(question);
            }
            catch (Exception e)
            {
                throw new DatabaseException(e.Message);
            }
        }