Exemple #1
0
        /*Обработчик события нажатия на кнопку РЕГИСТРАЦИЯ
         *
         */
        private async void registration_Click(object sender, EventArgs e)
        {
            string log = login_reg.Text;
            string pas = password_reg.Text;

            //проверка длины логина
            if (log.Length < 4 || log.Length > 12)
            {
                MessageBox.Show("Логин должен содержать от 4 до 12 символов.", "Ошибка регистрации", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            //проверка длины пароля
            else if (pas.Length < 5 || pas.Length > 10)
            {
                MessageBox.Show("Пароль должен содержать от 5 до 10 символов.", "Ошибка регистрации", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            //проверка логина
            else if (!char.IsLetter(log[0]))
            {
                MessageBox.Show("Логин не должен начинаться с цифры", "Ошибка регистрации", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            else
            {
                SqlCommand sqlCommand = null;
                try
                {
                    //открытие соединения к БД
                    string connectionString = "Data Source=localhost;Initial Catalog=Puzzle;Integrated Security=True";
                    sqlConnection = new SqlConnection(connectionString);
                    await sqlConnection.OpenAsync();

                    //запрос в БД на запись пользователя
                    sqlCommand = new SqlCommand("INSERT INTO [User] (login, password) VALUES(@login, @password)", sqlConnection);
                    sqlCommand.Parameters.AddWithValue("login", log);
                    sqlCommand.Parameters.AddWithValue("password", pas);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ошибка подключения к базе данных.");
                }
                if (isUserExists())
                {
                    return;
                }

                else if (sqlCommand.ExecuteNonQuery() == 1)
                {
                    //переход к меню пользователя
                    UserMenu s = new UserMenu(log);
                    s.Show();
                    this.Hide();
                }
            }
        }
Exemple #2
0
        /* обработчик события нажатия на кнопку ВХОД
         */
        private void enter_Click(object sender, EventArgs e)
        {
            string log = login_enter.Text;
            string pas = password_enter.Text;

            string connectionString = "Data Source=localhost;Initial Catalog=Puzzle;Integrated Security=True";

            sqlConnection = new SqlConnection(connectionString);
            try
            {
                //откывается соединение к БД
                sqlConnection.Open();
                if (isUserExistsEnter())
                {
                    UserMenu s = new UserMenu(log);
                    s.Show();
                    this.Hide();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка подключения к базе данных.");
            }
        }