コード例 #1
0
        private void addUser_Click(object sender, EventArgs e)
        {
            var userForm = new UserForm(new UserModel(), EditMode.Create);

            userForm.Show();
        }
コード例 #2
0
        private void enterButton_Click(object sender, EventArgs e) // при нажатии на кнопку ВОЙТИ
        {
            if (loginTextBox.Text == "" || passTextBox.Text == "") // заполнение строк логина и пароля обязательно
            {
                MessageBox.Show("Заполните пустые строки!", "Ошибка", MessageBoxButtons.OK);
            }
            else
            {
                SqlConnection conn          = DBUtils.GetDBConnection();
                string        sqlExpression = $"SELECT * FROM users WHERE login = '******' AND password = '******'";
                int           user_id       = 0;
                try
                {
                    conn.Open();
                    SqlCommand    command = new SqlCommand(sqlExpression, conn);
                    SqlDataReader reader  = command.ExecuteReader();
                    if (reader.HasRows)                                  // если пользователь найден
                    {
                        int userRole = getUserData(reader, ref user_id); // проверяем его роль и доступ

                        if (userRole == Constants.ACCESS_ERROR)
                        {
                            MessageBox.Show("Доступ запрещен.", "Ошибка", MessageBoxButtons.OK);
                        }
                        else if (userRole == Constants.ADMIN_ROLE)
                        {
                            clearInputs();
                            AdminForm adminForm = new AdminForm();
                            adminForm.Show();
                            this.Hide();
                        }
                        else if (userRole == Constants.USER_ROLE)
                        {
                            clearInputs();
                            CurrentUser.Id = user_id; //сохраняем id текущего пользователя
                            UserForm userForm = new UserForm();
                            userForm.Show();
                            this.Hide();
                        }
                        else
                        {
                            clearInputs();
                            CurrentUser.Id = user_id; //сохраняем id текущего пользователя
                            DocForm docForm = new DocForm();
                            docForm.Show();
                            this.Hide();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Такого пользователя не существует. Введите данные снова.", "Ошибка", MessageBoxButtons.OK);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK);
                }
                finally
                {
                    // Гарантировать освобождение подключения
                    conn.Close();
                }
            }
        }