public QuestionForm(Question question)
 {
     InitializeComponent();
     Text = "Изменить";
     questionText.Text = question.QuestionText;
     questionType.SelectedIndex = question.QuestionType - 1;
     foreach (Answer ansewer in question.Ansewers)
     {
         answersList.Items.Add(ansewer);
     }
 }
 private void button4_Click(object sender, EventArgs e)
 {
     Question = new Question();
     Question.QuestionText = questionText.Text;
     Question.QuestionType = questionType.SelectedIndex + 1;
     Question.Ansewers = new List<Answer>();
     foreach (Answer ansewer in answersList.Items)
     {
         Question.Ansewers.Add(ansewer);
     }
     DialogResult = System.Windows.Forms.DialogResult.OK;
     Close();
 }
        public List<Question> Questions()
        {
            List<Question> questions = new List<Question>();

            using (SqlCommand sqlCommand = new SqlCommand(SelectQuestions, _sqlConnection))
            {
                using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
                {
                    if (sqlDataReader.HasRows)
                    {
                        while (sqlDataReader.Read())
                        {
                            Question question = new Question();
                            question.Id = int.Parse(sqlDataReader["Id"].ToString());
                            question.QuestionText = sqlDataReader["Question_Text"].ToString();
                            question.QuestionType = int.Parse(sqlDataReader["Question_Type"].ToString());
                            question.Ansewers = new List<Answer>();
                            questions.Add(question);
                        }
                    }
                }
                foreach (Question question in questions)
                {
                    using (SqlCommand cmd = new SqlCommand(SelectAnswers, _sqlConnection))
                    {
                        cmd.Parameters.AddWithValue("id", question.Id);
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                question.Ansewers.Add(new Answer()
                                {
                                    Id = int.Parse(reader["Id"].ToString()),
                                    AnswerText = reader["Answer_Text"].ToString(),
                                    ClassId = int.Parse(reader["Class_Id"].ToString()),
                                    QuestionId = int.Parse(reader["Question_Id"].ToString())
                                });
                            }
                        }
                    }
                }
                return questions;
            }
        }
        public void DeleteQuestion(Question question)
        {
            using (SqlCommand sqlCommand = new SqlCommand(RemoveAnswer, _sqlConnection))
            {
                sqlCommand.Parameters.AddWithValue("@id", question.Id);
                sqlCommand.ExecuteNonQuery();
            }
            using (SqlCommand sqlCommand = new SqlCommand(RemoveQuestion, _sqlConnection))
            {
                sqlCommand.Parameters.AddWithValue("@id", question.Id);

                if (sqlCommand.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("Удалено");
                }
                else
                {
                    MessageBox.Show("Ошибка при удалении");
                }
            }
        }
        public void ChangeQuestion(Question oldQuestion, Question newQuestion)
        {
            using (SqlCommand sqlCommand = new SqlCommand(RemoveAnswer, _sqlConnection))
            {
                sqlCommand.Parameters.AddWithValue("@id", oldQuestion.Id);
                sqlCommand.ExecuteNonQuery();
            }

            using (SqlCommand sqlCommand = new SqlCommand(UpdateQuestion, _sqlConnection))
            {
                sqlCommand.Parameters.AddWithValue("@questionText", newQuestion.QuestionText);
                sqlCommand.Parameters.AddWithValue("@questionType", newQuestion.QuestionType);
                sqlCommand.Parameters.AddWithValue("@id", oldQuestion.Id);
                sqlCommand.ExecuteNonQuery();
            }

            foreach (Answer ansewer in newQuestion.Ansewers)
            {
                using (SqlCommand cmd = new SqlCommand(InsertAnswer, _sqlConnection))
                {

                    cmd.Parameters.AddWithValue("@answerText", ansewer.AnswerText);
                    cmd.Parameters.AddWithValue("@classId", ansewer.ClassId);
                    cmd.Parameters.AddWithValue("@questionId", oldQuestion.Id);
                    cmd.ExecuteNonQuery();
                }
            }

            MessageBox.Show("Изменено");
        }
        public void AddQuestion(Question question)
        {
            using (SqlCommand sqlCommand = new SqlCommand(InsertQuestion, _sqlConnection))
            {
                sqlCommand.Parameters.AddWithValue("@questionText", question.QuestionText);
                sqlCommand.Parameters.AddWithValue("@questionType", question.QuestionType);

                if (sqlCommand.ExecuteNonQuery() > 0)
                {
                    foreach (Answer ansewer in question.Ansewers)
                    {
                        using (SqlCommand cmd = new SqlCommand(InsertAnswers, _sqlConnection))
                        {

                            cmd.Parameters.AddWithValue("@answerText", ansewer.AnswerText);
                            cmd.Parameters.AddWithValue("@classId", ansewer.ClassId);
                            cmd.ExecuteNonQuery();
                        }
                    }
                    //                MessageBox.Show("Добвленно");
                }
                else
                {
                    MessageBox.Show("Ошибка при добавлении");
                }
            }
        }