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; namespace WindowsFormsApp13 { public partial class AddForm : Form { private string connectionString; private Form2 mainForm; public AddForm(string connectionString, Form2 form) { InitializeComponent(); this.connectionString = connectionString; mainForm = form; } private void buttonAdd_Click(object sender, EventArgs e) { string newBookName = textBoxNewBookName.Text; if (!string.IsNullOrEmpty(newBookName)) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); string query = "INSERT INTO Books(name, id_autor, id_style) VALUES (@Name, @AutorId, @StyleId)"; int autorId = Int32.Parse(textBoxAutorId.Text); int styleId = Int32.Parse(textBoxStyleId.Text); using (MySqlCommand command = new MySqlCommand(query, connection)) { command.Parameters.AddWithValue("@Name", newBookName); command.Parameters.AddWithValue("@AutorId", autorId); command.Parameters.AddWithValue("@StyleId", styleId); command.ExecuteNonQuery(); } } MessageBox.Show("Новая книга успешно добавлена!"); mainForm.DisplayDataFromDB(); this.Close(); } else { MessageBox.Show("Wrong"); } //using (MySqlConnection connection = new MySqlConnection(connectionString)) //{ // connection.Open(); // var nameBook = textBoxNewBookName.Text; // var authorBook = textBoxAutorId.Text; // var styleBook = textBoxStyleId.Text; // var addQuery = $"INSERT INTO `cargo` (`nameBook`, `authorBook`, `authorBook`) values ('{nameBook}', '{authorBook}', '{styleBook}')"; //} } private void textBoxNewBookName_TextChanged(object sender, EventArgs e) { } } }