コード例 #1
0
        private void loadNewQuestion()
        {
            //wersja z Sql server
            //SqlConnection conn = new SqlConnection("Server=DESKTOP-BO91STR\\SQLEXPRESS;Database=Quiz;Integrated Security=true");
            //conn.Open();

            //wersja z azure

            /*SqlConnection conn = null;
             * try
             * {
             *  var cb = new SqlConnectionStringBuilder();
             *  cb.DataSource = "serv2703.database.windows.net";
             *  cb.PersistSecurityInfo = false;
             *  cb.MultipleActiveResultSets = false;
             *  cb.Encrypt = true;
             *  cb.TrustServerCertificate = false;
             *  cb.ConnectTimeout = 30;
             *  cb.UserID = "adam";
             *  cb.Password = "******";
             *  cb.InitialCatalog = "Quiz";
             *
             *  conn = new SqlConnection(cb.ConnectionString);
             *  conn.Open();
             * }
             * catch (SqlException e)
             * {
             *  Console.WriteLine(e.ToString());
             * }*/

            //część uniwersalna dla sql
            // SqlCommand cmd = new SqlCommand(
            //     "Select q.Description from Questions2 q where q.Topic='"+scenario+"' and q.Difficulty="+currentQuestion+"", conn);
            // SqlDataReader reader = cmd.ExecuteReader();
            // while (reader.Read())
            // {
            //     Quest.Text = reader.GetString(0);
            //     Question.Text = "Question" + currentQuestion;

            // }
            // cmd=new SqlCommand(
            //     "select a.[Description] , a.IsCorrect from Questions2 q join Answers2 a on a.QuestionId = q.QuestionId where q.Topic='" + scenario + "' and q.Difficulty =" + currentQuestion + "", conn);
            // reader.Close();
            //reader = cmd.ExecuteReader();
            // int currentans = 0;
            // while (reader.Read())
            // {
            //     answers.ElementAt(currentans).Text=reader.GetString(0);
            //     if (reader.GetBoolean(1))
            //     {
            //         correctAns = answers.ElementAt(currentans);
            //     }
            //     currentans++;
            // }
            // Shuffle(answers);
            // ansA = answers.ElementAt(0);
            // ansB = answers.ElementAt(1);
            // ansC = answers.ElementAt(2);
            // ansD = answers.ElementAt(3);
            // foreach(Button b in answers)
            // {
            //     if (b.Equals(correctAns))
            //     {
            //         correctNumb = answers.IndexOf(b);
            //     }
            // }
            // reader.Close();
            // conn.Close();
            toJson          tJ = new toJson();
            List <Question> lq = new List <Question>();
            List <Answer>   la = new List <Answer>();

            lq = tJ.serializeQuestions();
            la = tJ.serializeAnswers();
            Question currQ = null;

            foreach (Question q in lq)
            {
                if (q.Topic == scenario && q.Difficulty == currentQuestion)
                {
                    currQ         = q;
                    Quest.Text    = q.Description;
                    Question.Text = "Question" + currentQuestion;
                    break;
                }
            }
            int currentans = 0;

            foreach (Answer a in la)
            {
                if (a.QuestionId == currQ.QuestionId)
                {
                    answers.ElementAt(currentans).Text = a.Description;
                    if (a.IsCorrect == 1)
                    {
                        correctAns = answers.ElementAt(currentans);
                    }
                    currentans++;
                }
            }
            Shuffle(answers);
            ansA = answers.ElementAt(0);
            ansB = answers.ElementAt(1);
            ansC = answers.ElementAt(2);
            ansD = answers.ElementAt(3);
            foreach (Button b in answers)
            {
                if (b.Equals(correctAns))
                {
                    correctNumb = answers.IndexOf(b);
                }
            }
        }