118 lines
4.4 KiB
C#
118 lines
4.4 KiB
C#
using MySql.Data.MySqlClient;
|
||
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.Xml.Linq;
|
||
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||
|
||
namespace fsh
|
||
{
|
||
public partial class AddBooksForm : Form
|
||
{
|
||
string connStr = "Server=cfif31.ru;Port=3306;Database=ISPr25-21_KorotinDV_37;Uid=ISPr25-21_KorotinDV;Pwd=ISPr25-21_KorotinDV; SslMode=none; ";
|
||
|
||
public AddBooksForm()
|
||
{
|
||
InitializeComponent();
|
||
LoadAuthors(); // Загружаем авторов при открытии формы
|
||
}
|
||
|
||
// Метод загрузки авторов в ComboBox
|
||
private void LoadAuthors()
|
||
{
|
||
try
|
||
{
|
||
using (MySqlConnection conn = new MySqlConnection(connStr))
|
||
{
|
||
conn.Open();
|
||
string sql = "SELECT idavtori, name_A FROM avtori ORDER BY name_A";
|
||
|
||
MySqlCommand cmd = new MySqlCommand(sql, conn);
|
||
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
|
||
DataTable dt = new DataTable();
|
||
da.Fill(dt);
|
||
|
||
// Настраиваем ComboBox
|
||
cbAuthor.DataSource = dt;
|
||
cbAuthor.DisplayMember = "name_A";
|
||
cbAuthor.ValueMember = "idavtori";
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Ошибка загрузки авторов: " + ex.Message, "Критическая ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
private void button1_Click(object sender, EventArgs e)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(textBox1.Text))
|
||
{
|
||
MessageBox.Show("Введите название книги", "Ошибка ввода", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
textBox1.Focus();
|
||
return;
|
||
}
|
||
|
||
if (cbAuthor.SelectedValue == null)
|
||
{
|
||
MessageBox.Show("Выберите автора из списка", "Ошибка ввода", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
|
||
if (!decimal.TryParse(textBox2.Text.Replace(",", "."), out decimal price) || price < 0)
|
||
{
|
||
MessageBox.Show("Введите корректную цену (число >= 0)", "Ошибка ввода", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
textBox2.Focus();
|
||
return;
|
||
}
|
||
|
||
// 2. Подготовка данных
|
||
string bookName = textBox1.Text.Trim();
|
||
int authorId = Convert.ToInt32(cbAuthor.SelectedValue);
|
||
|
||
// 3. INSERT в базу данных
|
||
try
|
||
{
|
||
using (MySqlConnection conn = new MySqlConnection(connStr))
|
||
{
|
||
conn.Open();
|
||
string sql = "INSERT INTO books (name, avtor, price) VALUES (@name, @avtor, @price)";
|
||
MySqlCommand cmd = new MySqlCommand(sql, conn);
|
||
|
||
cmd.Parameters.AddWithValue("@name", bookName);
|
||
cmd.Parameters.AddWithValue("@avtor", authorId);
|
||
cmd.Parameters.AddWithValue("@price", price);
|
||
|
||
int rows = cmd.ExecuteNonQuery();
|
||
|
||
if (rows > 0)
|
||
{
|
||
MessageBox.Show("Книга успешно добавлена!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
this.DialogResult = DialogResult.OK;
|
||
this.Close();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось добавить книгу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Ошибка при добавлении: " + ex.Message, "Критическая ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
private void textBox1_TextChanged(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|