using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace ARM_spec_otdelenia { public partial class AddEditUslugi : Form { SqlConnection connection; SqlCommand command; SqlDataAdapter adapter; public AddEditUslugi() { InitializeComponent(); connection = new SqlConnection("Server=DESKTOP-2UUHBK8\\SQLEXPRESS; Database=Спец_отделение; Trusted_connection=true;"); command = new SqlCommand(); command.Connection = connection; command.CommandType = CommandType.Text; adapter = new SqlDataAdapter(command); AdminForm adminForm = new AdminForm(); } private void saveBtn_MouseMove(object sender, MouseEventArgs e) { saveBtn.ForeColor = Color.FromArgb(24, 186, 96); saveBtn.FlatAppearance.BorderColor = Color.FromArgb(24, 186, 96); } private void backBtn_MouseMove(object sender, MouseEventArgs e) { backBtn.ForeColor = Color.FromArgb(24, 186, 96); backBtn.FlatAppearance.BorderColor = Color.FromArgb(24, 186, 96); } private void backBtn_MouseLeave(object sender, EventArgs e) { backBtn.ForeColor = Color.Black; backBtn.FlatAppearance.BorderColor = Color.Black; } private void saveBtn_MouseLeave(object sender, EventArgs e) { saveBtn.ForeColor = Color.Black; saveBtn.FlatAppearance.BorderColor = Color.Black; } private void saveBtn_Click(object sender, EventArgs e) { if (UslugiClass.add == true) { if (string.IsNullOrEmpty(nameText.Text) || string.IsNullOrEmpty(priceText.Text)) { MessageBox.Show("Не все поля заполнены!"); return; } else { if (MessageBox.Show("Вы действительно хотите добавить запись?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { try { connection.Open(); command.CommandText = "INSERT INTO Услуги(Наименование_услуги, Стоимость) VALUES(@name, @pr)"; command.Parameters.AddWithValue("@name", nameText.Text); command.Parameters.AddWithValue("@pr", Math.Round(Decimal.Parse(priceText.Text), 2)); command.ExecuteReader(); command.Parameters.Clear(); connection.Close(); this.Close(); MessageBox.Show("Запись успешно добавлена!"); } catch (Exception ex) { MessageBox.Show(ex.Message); command.Parameters.Clear(); connection.Close(); } } else { } } } if (UslugiClass.change == true) { if (string.IsNullOrEmpty(nameText.Text) || string.IsNullOrEmpty(priceText.Text)) { MessageBox.Show("Не все поля заполнены!"); return; } else { if (MessageBox.Show("Вы действительно хотите изменить запись?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { try { connection.Open(); command.CommandText = "UPDATE Услуги SET Наименование_услуги=@name, Стоимость=@pr WHERE ID=@id"; command.Parameters.AddWithValue("@name", nameText.Text); command.Parameters.AddWithValue("@pr", Math.Round(Decimal.Parse(priceText.Text), 2)); command.Parameters.AddWithValue("@id", UslugiClass.id); command.ExecuteReader(); command.Parameters.Clear(); connection.Close(); this.Close(); MessageBox.Show("Запись успешно изменена!"); } catch (Exception ex) { MessageBox.Show(ex.Message); command.Parameters.Clear(); connection.Close(); } } else { } } } } } }