/
DaNN26
/
sql-utility-manager
Обзор
Документация
Войти
/
DaNN26
/
sql-utility-manager
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Demo/AddForm.cs
166 строк
6 KB
DaNN
Добавьте файлы проекта.
25 май 2026, 17:03
25 май 2026, 17:03
9efb65a
Код
Авторство
О чём код?
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 Demo { public partial class AddForm : Form { DatabaseHelper db = new DatabaseHelper(); private string fullname; private string role; public AddForm(string fullname, string role) { InitializeComponent(); this.fullname = fullname; this.role = role; LoadCategory(); LoadSupplier(); LoadProducer(); LoadUnit(); } private void AddButton_Click(object sender, EventArgs e) { string title = titleTextBox.Text; string category = categoryComboBox.Text; string description = descTextBox.Text; string producer = producerComboBox.Text; string supplier = supplierComboBox.Text; string price = priceTextBox.Text; string unit = unitComboBox.Text; string amount = amountTextBox.Text; string discount = discountTextBox.Text; if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(category) || string.IsNullOrEmpty(description) || string.IsNullOrEmpty(producer) || string.IsNullOrEmpty(supplier) || string.IsNullOrEmpty(price) || string.IsNullOrEmpty(unit) || string.IsNullOrEmpty(amount) || string.IsNullOrEmpty(discount)) { MessageBox.Show("Заполните пустые поля!"); return; } if (!price.All(Char.IsDigit) || !amount.All(Char.IsDigit) || !discount.All(Char.IsDigit)) { MessageBox.Show("Цена, Количество на складе и Скидка должны быть численными значениями!"); return; } else if (discount.All(Char.IsDigit)) { if (Int32.Parse(discount) > 100) { MessageBox.Show("Скидка не может быть больше 100!"); return; } } string categoryId = db.GetData(@"SELECT id FROM category WHERE title = + '" + category + "'").Rows[0]["id"].ToString(); string producerId = db.GetData(@"SELECT id FROM producer WHERE title = + '" + producer + "'").Rows[0]["id"].ToString(); string supplierId = db.GetData(@"SELECT id FROM supplier WHERE title = + '" + supplier + "'").Rows[0]["id"].ToString(); string unitId = db.GetData(@"SELECT id FROM unit WHERE title = + '" + unit + "'").Rows[0]["id"].ToString(); string art = generateArt(); db.RunCommand($"INSERT INTO product (Артикул, Наименование_товара, Единица_измерения, Цена, Поставщик, Производитель, Категория_товара, Действующая_скидка, Кол_во_на_складе, Описание_товара) VALUES ('{art}', '{title}', '{unitId}', '{price}', '{supplierId}', '{producerId}', '{categoryId}', '{discount}', '{amount}', '{description}')"); MessageBox.Show($"Товар {title} успешно добавлен."); titleTextBox.Clear(); categoryComboBox.Text = ""; descTextBox.Clear(); producerComboBox.Text = ""; supplierComboBox.Text = ""; priceTextBox.Clear(); unitComboBox.Text = ""; amountTextBox.Clear(); discountTextBox.Clear(); } private string generateArt() { Random random = new Random(); while (true) { string art = random.Next().ToString(); DataTable data = db.GetData(@"SELECT * FROM product WHERE Артикул = '" + art + "'"); if (data.Rows.Count == 0) { return art; } } } private void LoadCategory() { DataTableReader categoryReader = db.GetData("SELECT title FROM category").CreateDataReader(); while (categoryReader.Read()) { categoryComboBox.Items.Add(categoryReader["title"].ToString()); } } private void LoadSupplier() { DataTableReader supplierReader = db.GetData("SELECT title FROM supplier").CreateDataReader(); while (supplierReader.Read()) { supplierComboBox.Items.Add(supplierReader["title"].ToString()); } } private void LoadProducer() { DataTableReader producerReader = db.GetData("SELECT title FROM producer").CreateDataReader(); while (producerReader.Read()) { producerComboBox.Items.Add(producerReader["title"].ToString()); } } private void LoadUnit() { DataTableReader unitReader = db.GetData("SELECT title FROM unit").CreateDataReader(); while (unitReader.Read()) { unitComboBox.Items.Add(unitReader["title"].ToString()); } } private void AddForm_Load(object sender, EventArgs e) { // TODO: данная строка кода позволяет загрузить данные в таблицу "marketDataSet.producer". При необходимости она может быть перемещена или удалена. this.producerTableAdapter.Fill(this.marketDataSet.producer); // TODO: данная строка кода позволяет загрузить данные в таблицу "marketDataSet.supplier". При необходимости она может быть перемещена или удалена. this.supplierTableAdapter.Fill(this.marketDataSet.supplier); } private void backButton_Click(object sender, EventArgs e) { MainForm mainForm = new MainForm(fullname, role); mainForm.Show(); this.Close(); } } }