using System; using System.Data; using System.Windows.Forms; using Npgsql; namespace PhotoStudioApp { public partial class ServiceForm : Form { private string _connectionString; public ServiceForm(string connectionString) { InitializeComponent(); _connectionString = connectionString; LoadServices(); } private void LoadServices() { using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString)) { conn.Open(); string sql = "select service_id, service_name, service_type, price from services"; NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(sql, conn); DataTable dt = new DataTable(); adapter.Fill(dt); // Скрываем service_id dgvServices.DataSource = dt; if (dgvServices.Columns.Contains("service_id")) { dgvServices.Columns["service_id"].Visible = false; } } } private void btnAdd_Click(object sender, EventArgs e) { // Простая форма для добавления string name = Microsoft.VisualBasic.Interaction.InputBox("Введите название услуги", "Добавление"); if (!string.IsNullOrEmpty(name)) { string type = Microsoft.VisualBasic.Interaction.InputBox("Введите тип услуги (photo, video, event)", "Добавление"); string priceStr = Microsoft.VisualBasic.Interaction.InputBox("Введите стоимость", "Добавление"); if (decimal.TryParse(priceStr, out decimal price)) { using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString)) { conn.Open(); string sql = "insert into services (service_name, service_type, price) values (@name, @type, @price)"; NpgsqlCommand cmd = new NpgsqlCommand(sql, conn); cmd.Parameters.AddWithValue("@name", name); cmd.Parameters.AddWithValue("@type", type); cmd.Parameters.AddWithValue("@price", price); cmd.ExecuteNonQuery(); MessageBox.Show("Услуга добавлена"); LoadServices(); } } else { MessageBox.Show("Некорректная стоимость"); } } } private void btnEdit_Click(object sender, EventArgs e) { if (dgvServices.SelectedRows.Count > 0) { DataRowView rowView = dgvServices.SelectedRows[0].DataBoundItem as DataRowView; if (rowView != null) { int serviceId = (int)rowView["service_id"]; string currentName = rowView["service_name"].ToString(); string currentType = rowView["service_type"].ToString(); string currentPrice = rowView["price"].ToString(); string name = Microsoft.VisualBasic.Interaction.InputBox("Название услуги", "Редактирование", currentName); string type = Microsoft.VisualBasic.Interaction.InputBox("Тип услуги", "Редактирование", currentType); string priceStr = Microsoft.VisualBasic.Interaction.InputBox("Стоимость", "Редактирование", currentPrice); if (decimal.TryParse(priceStr, out decimal price)) { using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString)) { conn.Open(); string sql = "update services set service_name = @name, service_type = @type, price = @price where service_id = @serviceId"; NpgsqlCommand cmd = new NpgsqlCommand(sql, conn); cmd.Parameters.AddWithValue("@name", name); cmd.Parameters.AddWithValue("@type", type); cmd.Parameters.AddWithValue("@price", price); cmd.Parameters.AddWithValue("@serviceId", serviceId); cmd.ExecuteNonQuery(); MessageBox.Show("Услуга обновлена"); LoadServices(); } } else { MessageBox.Show("Некорректная стоимость"); } } } else { MessageBox.Show("Выберите услугу для редактирования"); } } private void btnDelete_Click(object sender, EventArgs e) { if (dgvServices.SelectedRows.Count > 0) { DataRowView rowView = dgvServices.SelectedRows[0].DataBoundItem as DataRowView; if (rowView != null) { int serviceId = (int)rowView["service_id"]; if (MessageBox.Show("Удалить услугу?", "Подтверждение", MessageBoxButtons.YesNo) == DialogResult.Yes) { using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString)) { conn.Open(); string sql = "delete from services where service_id = @serviceId"; NpgsqlCommand cmd = new NpgsqlCommand(sql, conn); cmd.Parameters.AddWithValue("@serviceId", serviceId); cmd.ExecuteNonQuery(); MessageBox.Show("Услуга удалена"); LoadServices(); } } } } else { MessageBox.Show("Выберите услугу для удаления"); } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } } }