135 lines
7.2 KiB
C#
135 lines
7.2 KiB
C#
using Microsoft.Data.SqlClient;
|
||
using Npgsql;
|
||
using NpgsqlTypes;
|
||
using Renci.SshNet;
|
||
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 DeviceAdmin
|
||
{
|
||
public partial class FormDevices : Form
|
||
{
|
||
NpgsqlConnection connection = Program.connection;
|
||
private DataTable dataTable;
|
||
private DataTable dataTableKind = new();
|
||
private NpgsqlDataAdapter adapter;
|
||
private NpgsqlDataAdapter adapter2;
|
||
public FormDevices()
|
||
{
|
||
InitializeComponent();
|
||
LoadGridView();
|
||
adapter2 = new NpgsqlDataAdapter("SELECT * FROM kinds", connection);
|
||
adapter2.Fill(dataTableKind);
|
||
comboBoxKind.DataSource = dataTableKind;
|
||
}
|
||
|
||
private void LoadGridView()
|
||
{
|
||
adapter = new NpgsqlDataAdapter("SELECT devices.id, model, kinds.title, serial_number, production_date, warranty_period, condition, kit_id, kits.title FROM devices LEFT JOIN kinds ON devices.kind_id = kinds.id FULL OUTER JOIN kits ON devices.kit_id=kits.id;", connection);
|
||
dataTable = new DataTable();
|
||
adapter.Fill(dataTable);
|
||
dataGridView.DataSource = dataTable;
|
||
DataGridViewColumnCollection columns = dataGridView.Columns;
|
||
columns[0].HeaderText = "ID";
|
||
columns[1].HeaderText = "Модель";
|
||
columns[2].HeaderText = "Вид";
|
||
columns[3].HeaderText = "Сер. номер";
|
||
columns[4].HeaderText = "Дата изг.";
|
||
columns[5].HeaderText = "Гарантия";
|
||
columns[6].HeaderText = "Работает?";
|
||
columns[7].HeaderText = "ID компл.";
|
||
columns[8].HeaderText = "Комплект";
|
||
}
|
||
private void ButtonAdd_Click(object sender, EventArgs e)
|
||
{
|
||
if (checkBoxCondition.Checked == true)
|
||
{
|
||
NpgsqlCommand command = new NpgsqlCommand("INSERT INTO devices VALUES (nextval('seq_devices'), @model, @serial_number, @production_date, @warranty_period, '1', @kind_id, @kit_id);", connection);
|
||
command.Parameters.AddWithValue("@model", textBoxModel.Text);
|
||
command.Parameters.AddWithValue("@kind_id", (int)comboBoxKind.SelectedValue);
|
||
command.Parameters.AddWithValue("@serial_number", textBoxSerialNumber.Text);
|
||
command.Parameters.AddWithValue("@production_date", dateTimePickerProductionDate.Value.Date);
|
||
command.Parameters.AddWithValue("@warranty_period", int.Parse(numericUpDownWarrantyPeriod.Value.ToString()));
|
||
command.Parameters.AddWithValue("@kit_id", int.Parse(numericUpDownKit.Value.ToString()));
|
||
command.ExecuteNonQuery();
|
||
} else
|
||
{
|
||
NpgsqlCommand command = new NpgsqlCommand("INSERT INTO devices VALUES (nextval('seq_devices'), @model, @serial_number, @production_date, @warranty_period, '0', @kind_id, @kit_id);", connection);
|
||
command.Parameters.AddWithValue("@model", textBoxModel.Text);
|
||
command.Parameters.AddWithValue("@kind_id", (int)comboBoxKind.SelectedValue);
|
||
command.Parameters.AddWithValue("@serial_number", textBoxSerialNumber.Text);
|
||
command.Parameters.AddWithValue("@production_date", dateTimePickerProductionDate.Value.Date);
|
||
command.Parameters.AddWithValue("@warranty_period", int.Parse(numericUpDownWarrantyPeriod.Value.ToString()));
|
||
command.Parameters.AddWithValue("@kit_id", int.Parse(numericUpDownKit.Value.ToString()));
|
||
command.ExecuteNonQuery();
|
||
}
|
||
LoadGridView();
|
||
}
|
||
|
||
private void ButtonEdit_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count > 0)
|
||
{
|
||
if (checkBoxCondition.Checked == true)
|
||
{
|
||
int id = (int)dataGridView.SelectedRows[0].Cells[0].Value;
|
||
NpgsqlCommand command = new NpgsqlCommand("UPDATE devices SET model = @model, serial_number=@serial_number, production_date=@production_date, warranty_period=@warranty_period, condition='1', kind_id=@kind_id, kit_id=@kit_id WHERE Id = @Id", connection);
|
||
command.Parameters.AddWithValue("@Id", id);
|
||
command.Parameters.AddWithValue("@model", textBoxModel.Text);
|
||
command.Parameters.AddWithValue("@kind_id", (int)comboBoxKind.SelectedValue);
|
||
command.Parameters.AddWithValue("@serial_number", textBoxSerialNumber.Text);
|
||
command.Parameters.AddWithValue("@production_date", dateTimePickerProductionDate.Value.Date);
|
||
command.Parameters.AddWithValue("@warranty_period", int.Parse(numericUpDownWarrantyPeriod.Value.ToString()));
|
||
command.Parameters.AddWithValue("@kit_id", int.Parse(numericUpDownKit.Value.ToString()));
|
||
command.ExecuteNonQuery();
|
||
} else
|
||
{
|
||
int id = (int)dataGridView.SelectedRows[0].Cells[0].Value;
|
||
NpgsqlCommand command = new NpgsqlCommand("UPDATE devices SET model = @model, serial_number=@serial_number, production_date=@production_date, warranty_period=@warranty_period, condition='0', kind_id= @kind_id, kit_id=@kit_id WHERE Id = @Id", connection);
|
||
command.Parameters.AddWithValue("@Id", id);
|
||
command.Parameters.AddWithValue("@model", textBoxModel.Text);
|
||
command.Parameters.AddWithValue("@kind_id", (int)comboBoxKind.SelectedValue);
|
||
command.Parameters.AddWithValue("@serial_number", textBoxSerialNumber.Text);
|
||
command.Parameters.AddWithValue("@production_date", dateTimePickerProductionDate.Value.Date);
|
||
command.Parameters.AddWithValue("@warranty_period", int.Parse(numericUpDownWarrantyPeriod.Value.ToString()));
|
||
command.Parameters.AddWithValue("@kit_id", int.Parse(numericUpDownKit.Value.ToString()));
|
||
command.ExecuteNonQuery();
|
||
}
|
||
LoadGridView();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Please select a row to edit.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
private void ButtonDelete_Click(object sender, EventArgs e)
|
||
{
|
||
if (dataGridView.SelectedRows.Count > 0)
|
||
{
|
||
int id = (int)dataGridView.SelectedRows[0].Cells[0].Value;
|
||
NpgsqlCommand command = new NpgsqlCommand("DELETE FROM devices WHERE Id = @Id", connection);
|
||
command.Parameters.AddWithValue("@Id", id);
|
||
command.ExecuteNonQuery();
|
||
LoadGridView();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Please select a row to delete.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
private void ButtonRefresh_Click(object sender, EventArgs e)
|
||
{
|
||
LoadGridView();
|
||
}
|
||
}
|
||
}
|