コード例 #1
0
        private void BackToWindow(object sender, RoutedEventArgs e)
        {
            ChangeUsersData changeUsers = new ChangeUsersData();

            changeUsers.Show();
            this.Close();
        }
コード例 #2
0
        private void ChangeUsersData_Click(object sender, RoutedEventArgs e)
        {
            ChangeUsersData changeUsersData = new ChangeUsersData();

            changeUsersData.Show();
            this.Close();
        }
コード例 #3
0
        private void SaveChanges(object sender, RoutedEventArgs e)
        {
            SqlConnection connection = new SqlConnection(connectionString);

            connection.Open();
            if (criterion.Text != string.Empty && searchCriterion.Text != string.Empty)
            {
                int uC = Convert.ToInt32(searchCriterion.Text);
                if (criterion.Text == "Логин")
                {
                    if (changingCriterion.Text != CheckUser.CheckUserLogin(changingCriterion.Text))
                    {
                        MessageBox.Show(CheckUser.CheckUserLogin(changingCriterion.Text));
                        return;
                    }
                    string mySelectQuery = "SELECT UserLogin FROM Users WHERE [UserLogin] = '" + changingCriterion.Text + "'";
                    using (SqlDataAdapter dataAdapter = new SqlDataAdapter(mySelectQuery, connection)) // проверка логина на занятость
                    {
                        DataTable table = new DataTable();
                        dataAdapter.Fill(table);
                        if (table.Rows.Count > 0)
                        {
                            MessageBox.Show("Этот логин занят. Введите другой");
                            return;
                        }
                        else if (table.Rows.Count == 0)
                        {
                            using (SqlCommand lastCommnd = connection.CreateCommand()) // обновляем данные
                            {
                                lastCommnd.CommandText = "UPDATE Users SET UserLogin = @login WHERE UserCode = @code";
                                lastCommnd.Parameters.AddWithValue("@login", changingCriterion.Text);
                                lastCommnd.Parameters.AddWithValue("@code", uC);

                                lastCommnd.ExecuteNonQuery();
                            }
                        }
                    }
                }
                else if (criterion.Text == "Пароль")
                {
                    if (changingCriterion.Text != CheckUser.CheckUserPassword(changingCriterion.Text))
                    {
                        MessageBox.Show(CheckUser.CheckUserPassword(changingCriterion.Text));
                        return;
                    }
                    using (SqlCommand lastCommnd = connection.CreateCommand()) // обновляем данные
                    {
                        lastCommnd.CommandText = "UPDATE Users SET UserPassword = @password WHERE UserCode = @code";
                        lastCommnd.Parameters.AddWithValue("@password", changingCriterion.Text);
                        lastCommnd.Parameters.AddWithValue("@code", uC);

                        lastCommnd.ExecuteNonQuery();
                    }
                }
                else if (criterion.Text == "Статус администратора")
                {
                    if (changingCriterion.Text != CheckUser.CheckUserStatus(changingCriterion.Text))
                    {
                        MessageBox.Show(CheckUser.CheckUserStatus(changingCriterion.Text));
                        return;
                    }
                    using (SqlCommand lastCommnd = connection.CreateCommand()) // обновляем данные
                    {
                        lastCommnd.CommandText = "UPDATE Users SET AdministratorState = @status WHERE UserCode = @code";
                        lastCommnd.Parameters.AddWithValue("@status", changingCriterion.Text);
                        lastCommnd.Parameters.AddWithValue("@code", uC);

                        lastCommnd.ExecuteNonQuery();
                    }
                }
                else if (criterion.Text == "Имя")
                {
                    if (changingCriterion.Text != CheckUser.CheckUserName(changingCriterion.Text))
                    {
                        MessageBox.Show(CheckUser.CheckUserName(changingCriterion.Text));
                        return;
                    }
                    using (SqlCommand lastCommnd = connection.CreateCommand()) // обновляем данные
                    {
                        lastCommnd.CommandText = "UPDATE Users SET UserName = @name WHERE UserCode = @code";
                        lastCommnd.Parameters.AddWithValue("@name", changingCriterion.Text);
                        lastCommnd.Parameters.AddWithValue("@code", uC);

                        lastCommnd.ExecuteNonQuery();
                    }
                }
                else if (criterion.Text == "Фамилия")
                {
                    if (changingCriterion.Text != CheckUser.CheckUserSurname(changingCriterion.Text))
                    {
                        MessageBox.Show(CheckUser.CheckUserSurname(changingCriterion.Text));
                        return;
                    }
                    using (SqlCommand lastCommnd = connection.CreateCommand()) // обновляем данные
                    {
                        lastCommnd.CommandText = "UPDATE Users SET UserSurname = @surname WHERE UserCode = @code";
                        lastCommnd.Parameters.AddWithValue("@surname", changingCriterion.Text);
                        lastCommnd.Parameters.AddWithValue("@code", uC);

                        lastCommnd.ExecuteNonQuery();
                    }
                }
            }
            else
            {
                MessageBox.Show("Вы не выбрали данные для изменения.");
                return;
            }
            MessageBoxResult mboxResult = MessageBox.Show("Данные обновлены. Желаете изменить что-нибудь еще?", "Предупреждение", MessageBoxButton.YesNo);

            if (mboxResult == MessageBoxResult.No)
            {
                ChangeUsersData changeUsersData = new ChangeUsersData();
                changeUsersData.Show();
                this.Close();
            }
            connection.Close();
        }