Exemplo n.º 1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (criterion.Text == "Модель автомобиля")
            {
                if (searchCriterion.Text != CheckCar.CheckCarModel(searchCriterion.Text))
                {
                    System.Windows.MessageBox.Show(CheckCar.CheckCarModel(searchCriterion.Text));
                    return;
                }
            }
            else if (criterion.Text == "Цвет автомобиля")
            {
                if (searchCriterion.Text != CheckCar.CheckCarColor(searchCriterion.Text))
                {
                    System.Windows.MessageBox.Show(CheckCar.CheckCarColor(searchCriterion.Text));
                    return;
                }
            }
            else if (criterion.Text == "Номер автомобиля")
            {
                if (searchCriterion.Text != CheckCar.CheckCarStateNumber(searchCriterion.Text))
                {
                    System.Windows.MessageBox.Show(CheckCar.CheckCarStateNumber(searchCriterion.Text));
                    return;
                }
            }
            else if (criterion.Text == "Фамилия владельца")
            {
                if (searchCriterion.Text != CheckViolator.CheckViolatorSurname(searchCriterion.Text))
                {
                    System.Windows.MessageBox.Show(CheckViolator.CheckViolatorSurname(searchCriterion.Text));
                    return;
                }
            }
            else if (criterion.Text == "Имя владельца")
            {
                if (searchCriterion.Text != CheckViolator.CheckViolatorName(searchCriterion.Text))
                {
                    System.Windows.MessageBox.Show(CheckViolator.CheckViolatorName(searchCriterion.Text));
                    return;
                }
            }
            else if (criterion.Text == "Отчество владельца")
            {
                if (searchCriterion.Text != CheckViolator.CheckViolatorPatronymic(searchCriterion.Text))
                {
                    System.Windows.MessageBox.Show(CheckViolator.CheckViolatorPatronymic(searchCriterion.Text));
                    return;
                }
            }
            else
            {
                System.Windows.MessageBox.Show("Вы не выбрали критерий поиска.");
                return;
            }
            StreamWriter writeCriterion = new StreamWriter("Criterion.txt");

            writeCriterion.Write(criterion.Text);
            writeCriterion.Close();

            StreamWriter writeSearchCriterion = new StreamWriter("SearchCriterion.txt");

            writeSearchCriterion.Write(searchCriterion.Text);
            writeSearchCriterion.Close();

            ViolatorsCarsSearchResult violatorsCarsSearchResult = new ViolatorsCarsSearchResult();

            violatorsCarsSearchResult.Show();
            this.Close();
        }
        private void SaveChanges(object sender, RoutedEventArgs e)
        {
            if (criterion.Text != string.Empty && searchCriterion.Text != string.Empty)
            {
                if (criterion.Text == "Марка автомобиля")
                {
                    if (changingCriterion.Text != CheckCar.CheckCarModel(changingCriterion.Text))
                    {
                        MessageBox.Show(CheckCar.CheckCarModel(changingCriterion.Text));
                        return;
                    }
                    string        query         = @"SELECT CarModelCode FROM CarModel WHERE CarModelName = '" + changingCriterion.Text + "'";
                    int           carModelCode  = 0;
                    SqlConnection sqlConnection = new SqlConnection(connectionString);
                    sqlConnection.Open();
                    SqlCommand    sqlCommand    = new SqlCommand(query, sqlConnection);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    if (sqlDataReader.HasRows)
                    {
                        while (sqlDataReader.Read())
                        {
                            carModelCode = Convert.ToInt32(sqlDataReader[0].ToString());
                        }
                    }
                    else
                    {
                        MessageBox.Show("Такой марки автомобиля нет в базе.");
                        return;
                    }
                    sqlDataReader.Close();
                    sqlConnection.Close();
                    using (SqlConnection updateCarModel = new SqlConnection(connectionString))
                        using (SqlCommand lastCommnd = updateCarModel.CreateCommand())
                        {
                            lastCommnd.CommandText = "UPDATE ViolatorCar SET CarModelCode = @carModel WHERE CarCode = @code";

                            lastCommnd.Parameters.AddWithValue("@carModel", carModelCode);
                            lastCommnd.Parameters.AddWithValue("@code", Convert.ToInt32(searchCriterion.Text));

                            updateCarModel.Open();
                            lastCommnd.ExecuteNonQuery();
                            updateCarModel.Close();
                        }
                }
                else if (criterion.Text == "Цвет автомобиля")
                {
                    if (changingCriterion.Text != CheckCar.CheckCarColor(changingCriterion.Text))
                    {
                        MessageBox.Show(CheckCar.CheckCarColor(changingCriterion.Text));
                        return;
                    }
                    string        query         = @"SELECT ColorCode FROM Color WHERE ColorName = '" + changingCriterion.Text + "'";
                    int           colorCode     = 0;
                    SqlConnection sqlConnection = new SqlConnection(connectionString);
                    sqlConnection.Open();
                    SqlCommand    sqlCommand    = new SqlCommand(query, sqlConnection);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    if (sqlDataReader.HasRows)
                    {
                        while (sqlDataReader.Read())
                        {
                            colorCode = Convert.ToInt32(sqlDataReader[0].ToString());
                        }
                    }
                    else
                    {
                        MessageBox.Show("Такого цвета нет в базе.");
                        return;
                    }
                    sqlDataReader.Close();
                    sqlConnection.Close();
                    using (SqlConnection updateColor = new SqlConnection(connectionString))
                        using (SqlCommand lastCommnd = updateColor.CreateCommand())
                        {
                            lastCommnd.CommandText = "UPDATE ViolatorCar SET ColorCode = @color WHERE CarCode = @code";

                            lastCommnd.Parameters.AddWithValue("@color", colorCode);
                            lastCommnd.Parameters.AddWithValue("@code", Convert.ToInt32(searchCriterion.Text));

                            updateColor.Open();
                            lastCommnd.ExecuteNonQuery();
                            updateColor.Close();
                        }
                }
                else if (criterion.Text == "Номер автомобиля")
                {
                    if (changingCriterion.Text != CheckCar.CheckCarStateNumber(changingCriterion.Text))
                    {
                        MessageBox.Show(CheckCar.CheckCarStateNumber(changingCriterion.Text));
                        return;
                    }
                    SqlConnection sqlConnection = new SqlConnection(connectionString);
                    sqlConnection.Open();
                    string        findReplays   = "SELECT CarCode FROM ViolatorCar WHERE CarStatetNumber = '" + changingCriterion.Text + "'";
                    SqlCommand    sqlCommand3   = new SqlCommand(findReplays, sqlConnection);
                    SqlDataReader sqlDataReader = sqlCommand3.ExecuteReader();
                    if (sqlDataReader.HasRows)
                    {
                        MessageBox.Show("Вы ввели уже занятый номер автомобиля.");
                        return;
                    }
                    else
                    {
                        using (SqlConnection updateStateNumber = new SqlConnection(connectionString))
                            using (SqlCommand lastCommnd = updateStateNumber.CreateCommand())
                            {
                                lastCommnd.CommandText = "UPDATE ViolatorCar SET CarStatetNumber = @number WHERE CarCode = @code";

                                lastCommnd.Parameters.AddWithValue("@number", changingCriterion.Text);
                                lastCommnd.Parameters.AddWithValue("@code", Convert.ToInt32(searchCriterion.Text));

                                updateStateNumber.Open();
                                lastCommnd.ExecuteNonQuery();
                                updateStateNumber.Close();
                            }
                    }
                    sqlConnection.Close();
                }
                else if (criterion.Text == "Код владельца")
                {
                    if (changingCriterion.Text == string.Empty)
                    {
                        MessageBox.Show("Вы не указали код нарушителя.");
                    }
                    else
                    {
                        char[] violatorCodeArray = changingCriterion.Text.ToCharArray();
                        for (int i = 0; i < violatorCodeArray.Length; i++)
                        {
                            if (!char.IsDigit(violatorCodeArray[i]))
                            {
                                MessageBox.Show("Вы указали в коде недопустимые символы.");
                                return;
                            }
                        }
                    }
                    string        query         = @"SELECT ViolatorCode FROM Violator WHERE ViolatorCode = '" + changingCriterion.Text + "'";
                    int           violatorCode  = 0;
                    SqlConnection sqlConnection = new SqlConnection(connectionString);
                    sqlConnection.Open();
                    SqlCommand    sqlCommand    = new SqlCommand(query, sqlConnection);
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    if (sqlDataReader.HasRows)
                    {
                        while (sqlDataReader.Read())
                        {
                            violatorCode = Convert.ToInt32(sqlDataReader[0].ToString());
                        }
                    }
                    else
                    {
                        MessageBoxResult meboxResult = MessageBox.Show("Такого нарушителя нет в списке. Можете добавить нового нарушителя.", "Предупреждение", MessageBoxButton.YesNo);
                        if (meboxResult == MessageBoxResult.Yes)
                        {
                            AddViolator addViolator = new AddViolator();
                            addViolator.Show();
                            this.Close();
                        }
                        return;
                    }
                    sqlDataReader.Close();
                    sqlConnection.Close();
                    using (SqlConnection updateViolatorCode = new SqlConnection(connectionString))
                        using (SqlCommand lastCommnd = updateViolatorCode.CreateCommand())
                        {
                            lastCommnd.CommandText = "UPDATE ViolatorCar SET ViolatorCode = @violatorCode WHERE CarCode = @code";
                            lastCommnd.Parameters.AddWithValue("@code", Convert.ToInt32(searchCriterion.Text));
                            lastCommnd.Parameters.AddWithValue("@violatorCode", violatorCode);

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

            if (mboxResult == MessageBoxResult.No)
            {
                ChangeViolatorsCars changeViolarorsCars = new ChangeViolatorsCars();
                changeViolarorsCars.Show();
                this.Close();
            }
        }