private void AddButton_Click(object sender, RoutedEventArgs e) { if (currencyName.Text != CheckCurrency.CheckCurrencyName(currencyName.Text)) { System.Windows.MessageBox.Show(CheckCurrency.CheckCurrencyName(currencyName.Text)); return; } if (currencyCourse.Text != CheckCurrency.CheckCurrencyCourse(currencyCourse.Text)) { System.Windows.MessageBox.Show(CheckCurrency.CheckCurrencyCourse(currencyCourse.Text)); return; } SqlConnection connection = new SqlConnection(connectionString); connection.Open(); string findReplays = "SELECT * FROM CurrencySell WHERE CurrencySellName = '" + currencyName.Text + "'"; using (SqlDataAdapter dataAdapter = new SqlDataAdapter(findReplays, connection)) { DataTable table = new DataTable(); dataAdapter.Fill(table); if (table.Rows.Count > 0) { System.Windows.MessageBox.Show("Вы не можете добавить данную валюту, так как она уже есть в базе."); return; } else if (table.Rows.Count == 0) { using (SqlCommand lastCommnd = connection.CreateCommand()) { lastCommnd.CommandText = "INSERT INTO CurrencySell (CurrencySellName, CurrencySellCourse) VALUES (@name, @course)"; lastCommnd.Parameters.AddWithValue("@name", currencyName.Text); lastCommnd.Parameters.AddWithValue("@course", Convert.ToDouble(currencyCourse.Text)); lastCommnd.ExecuteNonQuery(); } } } connection.Close(); System.Windows.MessageBox.Show("Валюта успешно добавлена."); }
private void SaveChanges(object sender, RoutedEventArgs e) { SqlConnection connection = new SqlConnection(connectionString); connection.Open(); if (criterion.Text != string.Empty && searchCriterion.Text != string.Empty) { int currencyCode = Convert.ToInt32(searchCriterion.Text); if (criterion.Text == "Название валюты") { if (changingCriterion.Text != CheckCurrency.CheckCurrencyName(changingCriterion.Text)) { System.Windows.MessageBox.Show(CheckCurrency.CheckCurrencyName(changingCriterion.Text)); return; } string mySelectQuery = "SELECT * FROM CurrencySell WHERE CurrencySellName = '" + changingCriterion.Text + "'"; using (SqlDataAdapter dataAdapter = new SqlDataAdapter(mySelectQuery, connection)) { DataTable table = new DataTable(); dataAdapter.Fill(table); if (table.Rows.Count > 0) { System.Windows.MessageBox.Show("Вы не можете изменить данные, так как эта валюта уже есть в базе."); return; } else if (table.Rows.Count == 0) { using (SqlCommand lastCommnd = connection.CreateCommand()) { lastCommnd.CommandText = "UPDATE CurrencySell SET CurrencySellName = @name WHERE CurrencySellCode = @code"; lastCommnd.Parameters.AddWithValue("@name", changingCriterion.Text); lastCommnd.Parameters.AddWithValue("@code", currencyCode); lastCommnd.ExecuteNonQuery(); } } } } else if (criterion.Text == "Курс валюты") { if (changingCriterion.Text != CheckCurrency.CheckCurrencyCourse(changingCriterion.Text)) { System.Windows.MessageBox.Show(CheckCurrency.CheckCurrencyCourse(changingCriterion.Text)); return; } using (SqlCommand lastCommnd = connection.CreateCommand()) { lastCommnd.CommandText = "UPDATE CurrencySell SET CurrencySellCourse = @course WHERE CurrencySellCode = @code"; lastCommnd.Parameters.AddWithValue("@course", Convert.ToDouble(changingCriterion.Text)); lastCommnd.Parameters.AddWithValue("@code", currencyCode); lastCommnd.ExecuteNonQuery(); } } } else { System.Windows.MessageBox.Show("Вы не выбрали данные для изменения."); return; } MessageBoxResult mboxResult = System.Windows.MessageBox.Show("Данные обновлены. Желаете изменить что-нибудь еще?", "Предупреждение", MessageBoxButton.YesNo); if (mboxResult == MessageBoxResult.No) { ChangeCurrencySellCourse changeCurrencySellCourse = new ChangeCurrencySellCourse(); changeCurrencySellCourse.Show(); this.Close(); } connection.Close(); }