89 lines
3.2 KiB
C#

using Microsoft.Data.SqlClient;
using Npgsql;
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;
using Renci.SshNet;
using Renci.SshNet.Common;
using System.Runtime.InteropServices.JavaScript;
namespace DeviceAdmin
{
public partial class FormCabinets : Form
{
NpgsqlConnection connection = Program.connection;
private DataTable dataTable;
private NpgsqlDataAdapter adapter;
public FormCabinets()
{
InitializeComponent();
LoadGridView();
}
private void LoadGridView()
{
adapter = new NpgsqlDataAdapter("SELECT * FROM cabinets;", connection);
dataTable = new DataTable();
adapter.Fill(dataTable);
dataGridView.DataSource = dataTable;
DataGridViewColumnCollection columns = dataGridView.Columns;
columns[0].HeaderText = "ID";
columns[1].HeaderText = "Кабинет";
columns[2].HeaderText = "Корпус";
}
private void ButtonAdd_Click(object sender, EventArgs e)
{
NpgsqlCommand command = new NpgsqlCommand("INSERT INTO cabinets VALUES (nextval('seq_cabinets'), @Room, @Building);", connection);
command.Parameters.AddWithValue("@Room", textBoxRoom.Text);
command.Parameters.AddWithValue("@Building", int.Parse(numericUpDownBuilding.Value.ToString()));
command.ExecuteNonQuery();
LoadGridView();
}
private void ButtonEdit_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count > 0)
{
int id = (int)dataGridView.SelectedRows[0].Cells[0].Value;
NpgsqlCommand command = new NpgsqlCommand("UPDATE Cabinets SET Room = @Room, Building = @Building WHERE Id = @Id", connection);
command.Parameters.AddWithValue("@Id", id);
command.Parameters.AddWithValue("@Room", textBoxRoom.Text);
command.Parameters.AddWithValue("@Building", int.Parse(numericUpDownBuilding.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 Cabinets 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();
}
}
}