예제 #1
0
        private void новыйТестToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            NameTest NameTest = new NameTest(this);

            NameTest.ShowDialog();
        }
예제 #2
0
        private void сохранитьТестToolStripMenuItem_Click(object sender, EventArgs e)
        {
            save = true;
            if (listBox.Items.Count < 1)
            {
                save = false;
                MessageBox.Show("Тест пустой");
            }

            MessageBox.Show(ClassTotal.nameTest + "_" + ClassTotal.idTeacher);
            string        query      = "SELECT name FROM  sys.tables";
            SqlConnection connection = new SqlConnection(ClassTotal.connectionString);

            connection.Open();
            SqlCommand    command = new SqlCommand(query, connection);
            SqlDataReader reader  = command.ExecuteReader();

            while (reader.Read())
            {
                if (reader[0].ToString() == ClassTotal.nameTest + "_" + ClassTotal.idTeacher)
                {
                    save = false;
                    MessageBox.Show("Тест с таким названием уже создан.\nПереименуйте тест");
                    NameTest ntf = new NameTest(this);
                    ntf.Show();
                }
            }
            reader.Close();
            connection.Close();



            if (save == true)
            {
                query = "CREATE TABLE [" + ClassTotal.nameTest + "_" + ClassTotal.idTeacher +
                        "] ([ID_question] INT IDENTITY(1,1) NOT NULL PRIMARY KEY," +
                        "[Text_question] nvarchar(100) NOT NULL," +
                        "[Type_question] nvarchar(100) NOT NULL, " +
                        "[Answers] nvarchar(100) NOT NULL)";
                connection.Open();
                command = new SqlCommand(query, connection);
                command.ExecuteNonQuery();

                foreach (ClassQuestion item in listQuestion)
                {
                    string query1 = "INSERT INTO [" + ClassTotal.nameTest + "_" + ClassTotal.idTeacher +
                                    "] ([Text_question], [Type_question], [Answers]) " +
                                    "VALUES ('" + item.textQuestion + "', '" + item.typeQuestion + "', '" + item.answers + "')";
                    SqlCommand command1 = new SqlCommand(query1, connection);
                    command1.ExecuteNonQuery();
                    MessageBox.Show("Тест успешно сохранён");
                }
                DateTime dt = DateTime.Today;

                query = "INSERT INTO [Tests] ([ID_teacher], [Name_test], [Date_create]) VALUES ('" + ClassTotal.idTeacher + "','" + ClassTotal.nameTest + "','" +
                        dt.ToShortDateString() + "')";
                command = new SqlCommand(query, connection);
                command.ExecuteNonQuery();
                MessageBox.Show("Тест успешно записан в бд");
                connection.Close();
            }
        }