147 lines
5.2 KiB
C#
147 lines
5.2 KiB
C#
using MySql.Data.MySqlClient;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Diagnostics;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using System.Xml.Linq;
|
||
|
||
namespace fsh
|
||
{
|
||
public partial class EditBooksForm : Form
|
||
{
|
||
private int bookId;
|
||
string connStr = "Server=cfif31.ru;Port=3306;Database=ISPr25-21_KorotinDV_37;Uid=ISPr25-21_KorotinDV;Pwd=ISPr25-21_KorotinDV; SslMode=none";
|
||
|
||
public EditBooksForm(int id)
|
||
{
|
||
InitializeComponent();
|
||
bookId = id;
|
||
LoadBookData();
|
||
LoadAuthors();
|
||
}
|
||
|
||
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);
|
||
|
||
comboBox1.DataSource = dt;
|
||
comboBox1.DisplayMember = "name_A";
|
||
comboBox1.ValueMember = "idavtori";
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Ошибка загрузки авторов: " + ex.Message);
|
||
}
|
||
}
|
||
|
||
private void LoadBookData()
|
||
{
|
||
try
|
||
{
|
||
using (MySqlConnection conn = new MySqlConnection(connStr))
|
||
{
|
||
conn.Open();
|
||
string sql = "SELECT avtor, name, price FROM books WHERE idname = @id";
|
||
|
||
MySqlCommand cmd = new MySqlCommand(sql, conn);
|
||
cmd.Parameters.AddWithValue("@id", bookId);
|
||
|
||
MySqlDataReader reader = cmd.ExecuteReader();
|
||
|
||
if (reader.Read())
|
||
{
|
||
tbName.Text = reader["name"].ToString();
|
||
comboBox1.SelectedValue = reader["avtor"];
|
||
tbPrice.Text = reader["price"].ToString();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Книга не найдена!");
|
||
this.Close();
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Ошибка загрузки данных книги: " + ex.Message);
|
||
}
|
||
}
|
||
|
||
|
||
private void button1_Click(object sender, EventArgs e)
|
||
{
|
||
//string connStr = "Server=cfif31.ru;Port=3306;Database=ISPr25-21_KorotinDV_37;Uid=ISPr25-21_KorotinDV;Pwd=ISPr25-21_KorotinDV; SslMode=none";
|
||
//using (MySqlConnection conn = new MySqlConnection(connStr))
|
||
//{
|
||
// conn.Open();
|
||
// string sql = "UPDATE books SET name = @name, avtor = @avtor, price = @price WHERE idname = @idname";
|
||
// MySqlCommand cmd = new MySqlCommand(sql, conn);
|
||
// cmd.Parameters.AddWithValue("@idname", bookId);
|
||
// cmd.Parameters.AddWithValue("@name", tbName.Text);
|
||
// cmd.Parameters.AddWithValue("@avtor", comboBox1.Text);
|
||
// cmd.Parameters.AddWithValue("@price", decimal.Parse(tbPrice.Text));
|
||
// cmd.ExecuteNonQuery();
|
||
//}
|
||
//this.DialogResult = DialogResult.OK;
|
||
//this.Close();
|
||
|
||
|
||
|
||
try
|
||
{
|
||
using (MySqlConnection conn = new MySqlConnection(connStr))
|
||
{
|
||
conn.Open();
|
||
|
||
string sql = "UPDATE books SET name = @name, avtor = @avtor, price = @price WHERE idname = @id";
|
||
MySqlCommand cmd = new MySqlCommand(sql, conn);
|
||
|
||
cmd.Parameters.AddWithValue("@name", tbName.Text.Trim());
|
||
cmd.Parameters.AddWithValue("@avtor", comboBox1.SelectedValue);
|
||
cmd.Parameters.AddWithValue("@price", decimal.Parse(tbPrice.Text));
|
||
cmd.Parameters.AddWithValue("@id", bookId); // Важно: обновляем именно эту книгу
|
||
|
||
int rows = cmd.ExecuteNonQuery();
|
||
|
||
if (rows > 0)
|
||
{
|
||
MessageBox.Show("Данные успешно обновлены!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
this.DialogResult = DialogResult.OK;
|
||
this.Close();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось обновить данные.", "Ошибка");
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Ошибка при сохранении: " + ex.Message, "Критическая ошибка");
|
||
}
|
||
}
|
||
|
||
private void textBox1_TextChanged(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|