DemToday18/NeshinaDem3/NeshinaDem3/AddInf.cs
2025-11-19 16:11:00 +04:00

86 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 NeshinaDem3
{
public partial class AddInf : Form
{
public AddInf()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void txtName_TextChanged(object sender, EventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
}
private void btnSave_Click(object sender, EventArgs e) //СОХРАНИТЬ ПО КНОПКЕ
{
if (string.IsNullOrWhiteSpace(txtName.Text))
{
MessageBox.Show("Поле 'Название' обязательно для заполнения.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (!double.TryParse(txtCoefficient.Text, out double coefficient) || coefficient < 0)
{
MessageBox.Show("Поле 'Коэффициент' должно быть числом ≥ 0.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var conn = DB.GetInstance().GetConnection();
if (conn.State == ConnectionState.Closed)
conn.Open();
string insertQuery = @"
INSERT INTO TypeProduction (Name, Coefficient)
VALUES (@name, @coefficient)";
using (var cmd = new MySqlCommand(insertQuery, conn))
{
cmd.Parameters.AddWithValue("@name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@coefficient", coefficient);
try
{
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected > 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);
}
}
}
}
}