private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { try { using (DBWokrSql db = new DBWokrSql(Connection)) { var levels_ = (Dictionary <int, string>)db.GetLevels(); var topics_ = (Dictionary <int, string>)db.GetTopics(); var qq = new Dictionary <QuestionInfo, IEnumerable <AnswerInfo> >(); foreach (var item in db.GetIDQuestions(0, 0)) { var question_ = db.GetQuestion(item); var answers_ = db.GetAnswers(item); qq.Add(question_, answers_); } e.Cancel = false; e.Result = new { lev = levels_, topi = topics_, q = qq }; } } catch (Exception ex) { e.Cancel = false; e.Result = "Не має зв`язку з базою: " + ex.Message; } }
void SearchQuestion() { try { currentQuestions = new Dictionary <QuestionInfo, IEnumerable <AnswerInfo> >(); var a = (comboBox1.SelectedItem as Data) == null ? 0 : (comboBox1.SelectedItem as Data).ID; var b = (comboBox.SelectedItem as Data) == null ? 0 : (comboBox.SelectedItem as Data).ID; currentQuestions.Clear(); using (DBWokrSql db = new DBWokrSql(Connection)) { foreach (var item in db.GetIDQuestions(a, b)) { var question_ = db.GetQuestion(item); var answers_ = db.GetAnswers(item); currentQuestions.Add(question_, answers_); } } labelSearch.Content = "Знайдено :" + currentQuestions.Keys.Count; if (currentQuestions.Keys.Count == 0) { frameView.Navigate(new UserControlNewQuestion(Connection)); return; } currentQuest = 0; var Cquest = currentQuestions.Keys.ElementAt(currentQuest); frameView.Navigate(new UserControlNewQuestion(Connection, currentQuest, currentQuestions.Keys.Count, Cquest, currentQuestions[Cquest])); } catch (Exception ex) { MessageBox.Show("Search Question " + ex.Message); } }
private void button_Click(object sender, RoutedEventArgs e) { if (MessageBox.Show(String.Format("Питання буде {0} у базі. Ви впевнені?", ID == 0 ? "збережено" : "змінено"), "Увага", MessageBoxButton.OKCancel) != MessageBoxResult.OK) { return; } if (String.IsNullOrWhiteSpace(textBoxLevel.Text)) { MessageBox.Show("Перевірте поле Рівень"); return; } if (String.IsNullOrWhiteSpace(textBoxTopic.Text)) { MessageBox.Show("Перевірте поле Тема "); return; } if (String.IsNullOrWhiteSpace(textBoxQuestion.Text)) { MessageBox.Show("Перевірте поле питання "); return; } int idTick = 0; using (DBWokrSql db = new DBWokrSql(Connect)) { if (ID == 0) { //create question idTick = db.AddNewQuestion( new QuestionInfo() { ID = 0, Topic = textBoxTopic.Text, Info = textBoxInfo.Text, Level = textBoxLevel.Text, Question = textBoxQuestion.Text }, ans ); } else { //update question var questionCurrent = db.GetQuestion(ID); if ( !(questionCurrent.Info.Equals(textBoxInfo.Text.Replace('\'', ' ')) && questionCurrent.Level.Equals(textBoxLevel.Text) && questionCurrent.Question.Equals(textBoxQuestion.Text.Replace('\'', ' ')) && questionCurrent.Topic.Equals(textBoxTopic.Text)) ) { idTick = db.UpdateQuestion(questionCurrent); } if (idTick == 0) //question didn`t change { //update answers var answersCurrent = db.GetAnswers(ID); //get all new answers and check them by similar in database, if they were not found -- add them foreach (var item in ans) { if (answersCurrent.IsGetValue(item)) { var ss = answersCurrent.Where(s => s.ID == item.ID).First(); if (ss.IsTrue == item.IsTrue && ss.Text.Equals(item.Text.Replace('\'', ' '))) { continue; } else { db.UpdateAnswer(ID, item); } } else { int idAnswer = db.AddAnswer(item.Text); db.AddQuestionAnswer(ID, idAnswer, item.IsTrue); } } //marked all answers that not use foreach (var item in answersCurrent) { if (!ans.IsGetValue(item)) { db.DisableAnswer(item.ID); } } } else {// add answers to new question foreach (var item in ans) { int idAnswer = db.AddAnswer(item.Text); db.AddQuestionAnswer(idTick, idAnswer, item.IsTrue); } } } } MessageBox.Show("Питання було успішно збережено під номером " + (idTick == 0 ? ID : idTick)); }