| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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();
- }
- }
- }
|