using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Common; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace merkulov5 { public partial class MainForm : Form { private MySqlDataAdapter dataAdapter; private DataTable prodTable; private BindingSource bindingSource = new BindingSource(); public MainForm() { InitializeComponent(); LoadProdData(); LoadSortOptions(); } private void LoadProdData() // загрузка данных в дгв { try { DB db = new DB(); using (MySqlConnection connection = db.getConnection()) { connection.Open(); string query = "SELECT * FROM Librarian"; dataAdapter = new MySqlDataAdapter(query, connection); prodTable = new DataTable(); dataAdapter.Fill(prodTable); dataGridViewProd.RowTemplate.Height = 60; bindingSource.DataSource = prodTable; dataGridViewProd.DataSource = bindingSource; dataGridViewProd.AllowUserToAddRows = false; dataGridViewProd.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; dataGridViewProd.ReadOnly = true; // настройки дгв } } catch (MySqlException ex) { MessageBox.Show("Ошибка при подключении к базе данных или выполнении запроса: " + ex.Message); } catch (Exception ex) { MessageBox.Show("Произошла общая ошибка: " + ex.Message); } } private void MainForm_Load(object sender, EventArgs e) { } private async void btnAdd_Click(object sender, EventArgs e) { DB db = new DB(); DataTable table = new DataTable(); MySqlDataAdapter adapter = new MySqlDataAdapter(); db.openConnection(); MySqlCommand commandAdd = new MySqlCommand("INSERT INTO Librarian (Surname, Name, LastName, Password, Login)" + "VALUES (@Surname, @Name, @Last, @Pass, @Log);", db.getConnection()); commandAdd.Parameters.AddWithValue("@Surname", textBoxSurname.Text); commandAdd.Parameters.AddWithValue("@Name", textBoxName.Text); commandAdd.Parameters.AddWithValue("@Last", textBoxLast.Text); commandAdd.Parameters.AddWithValue("@Pass", textBoxLogin.Text); commandAdd.Parameters.AddWithValue("@Log", textBoxPass.Text); int rowsAffected = await commandAdd.ExecuteNonQueryAsync(); if (rowsAffected > 0) { MessageBox.Show("Библиотекарь добавлен."); LoadProdData(); } else { MessageBox.Show("Ошибка при добавлении библиотекаря"); db.closeConnection(); } } private void dataGridViewProd_Click(object sender, EventArgs e) { Surname.Text = dataGridViewProd.CurrentRow.Cells[1].Value.ToString(); Named.Text = dataGridViewProd.CurrentRow.Cells[2].Value.ToString(); LastName.Text = dataGridViewProd.CurrentRow.Cells[3].Value.ToString(); Log.Text = dataGridViewProd.CurrentRow.Cells[4].Value.ToString(); Pass.Text = dataGridViewProd.CurrentRow.Cells[5].Value.ToString(); } private void btnEdit_Click(object sender, EventArgs e) { DB db = new DB(); DataTable table = new DataTable(); MySqlDataAdapter adapter = new MySqlDataAdapter(); db.openConnection(); MySqlCommand commandEdit = new MySqlCommand(" UPDATE Librarian SET Surname = @Surname, Name = @Name, LastName = @Last, Login = @Login, Password = @Password WHERE idLibrarian = @id;", db.getConnection()); commandEdit.Parameters.AddWithValue("@id", Convert.ToInt32(dataGridViewProd.CurrentRow.Cells[0].Value)); commandEdit.Parameters.AddWithValue("@Surname", Surname.Text); commandEdit.Parameters.AddWithValue("@Name", Named.Text); commandEdit.Parameters.AddWithValue("@Last", LastName.Text); commandEdit.Parameters.AddWithValue("@Login", Log.Text); commandEdit.Parameters.AddWithValue("@Password", Pass.Text); int rowsAffected = commandEdit.ExecuteNonQuery(); if (rowsAffected > 0) { MessageBox.Show("Библиотекарь отредактирован."); LoadProdData(); } else { MessageBox.Show("Редактирование не удалось"); db.closeConnection(); } } private void button1_Click(object sender, EventArgs e) { DB db = new DB(); DataTable table = new DataTable(); MySqlDataAdapter adapter = new MySqlDataAdapter(); db.openConnection(); MySqlCommand commandDelete = new MySqlCommand("Delete FROM Librarian WHERE idLibrarian = @id;", db.getConnection()); commandDelete.Parameters.AddWithValue("@id", Convert.ToInt32(dataGridViewProd.CurrentRow.Cells[0].Value)); int rowsAffected = commandDelete.ExecuteNonQuery(); if (rowsAffected > 0) { MessageBox.Show("Библиотекарь удалена."); LoadProdData(); } else { MessageBox.Show("Не удалось удалить библиотекаря"); db.closeConnection(); } } private void LoadSortOptions() { comboBoxSort.Items.Add("По умолчанию"); comboBoxSort.Items.Add("Фамилия (а-я)"); comboBoxSort.Items.Add("Фамилия (я-а)"); comboBoxSort.SelectedIndex = 0; } private void comboBoxSort_SelectedIndexChanged(object sender, EventArgs e) { string sortOption = comboBoxSort.SelectedItem.ToString(); switch (sortOption) { case "По умолчанию": bindingSource.Sort = ""; break; case "Фамилия (а-я)": bindingSource.Sort = "Surname ASC"; break; case "Фамилия (я-а)": bindingSource.Sort = "Surname DESC"; break; } } } }