private void button2_Click(object sender, System.EventArgs e)
 {
     User oldUser = usersListBox.SelectedItem as User;
     if (oldUser != null)
     {
         User newUser = new User()
         {
             UserName = nameTextBox.Text,
             UserLogin = loginTextBox.Text,
             UserPassword = passwordTextBox.Text,
             IsAdmin = (isAdminCheckBox.Checked) ? 1 : 0
         };
         _baseWorker.ChangeUser(oldUser, newUser);
         RefreshUsers();
     }
 }
 private void button1_Click(object sender, System.EventArgs e)
 {
     User user = new User()
     {
         UserName = newNameTextBox.Text,
         UserLogin = newLoginTextBox.Text,
         UserPassword = newPasswordTextBox.Text,
         IsAdmin = (newIsAdminCheckBox.Checked) ? 1 : 0
     };
     _baseWorker.AddNewUser(user);
     RefreshUsers();
 }
        public void ChangeUser(User oldUser, User newUser)
        {
            using (SqlCommand sqlCommand = new SqlCommand(UpdateUser, _sqlConnection))
            {
                sqlCommand.Parameters.AddWithValue("@userName", newUser.UserName);
                sqlCommand.Parameters.AddWithValue("@userLogin", newUser.UserLogin);
                sqlCommand.Parameters.AddWithValue("@userPassword", newUser.UserPassword);
                sqlCommand.Parameters.AddWithValue("@userIsAdmin", newUser.IsAdmin);
                sqlCommand.Parameters.AddWithValue("@userId", oldUser.Id);

                if (sqlCommand.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("Пользователь изменен");
                }
                else
                {
                    MessageBox.Show("Ошибка при изменении");
                }
            }
        }
        public void DeleteUser(User user)
        {
            using (SqlCommand sqlCommand = new SqlCommand(RemoveUser, _sqlConnection))
            {
                sqlCommand.Parameters.AddWithValue("@userId", user.Id);

                if (sqlCommand.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("Пользователь удален");
                }
                else
                {
                    MessageBox.Show("Ошибка при удалении");
                }
            }
        }
        public void AddNewUser(User user)
        {
            using (SqlCommand sqlCommand = new SqlCommand(AddUser, _sqlConnection))
            {
                sqlCommand.Parameters.AddWithValue("@userName", user.UserName);
                sqlCommand.Parameters.AddWithValue("@userLogin", user.UserLogin);
                sqlCommand.Parameters.AddWithValue("@userPassword", user.UserPassword);
                sqlCommand.Parameters.AddWithValue("@userIsAdmin", user.IsAdmin);

                if (sqlCommand.ExecuteNonQuery() > 0)
                {
                  // MessageBox.Show("Добвленно");
                }
                else
                {
                    MessageBox.Show("Ошибка при добавлении");
                }
            }
        }