Files
2026-05-31 22:15:06 +03:00

147 lines
6.8 KiB
C#

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 Npgsql;
namespace demo26052026
{
public partial class Form4 : Form
{
static string ConnString = "Server=localhost;Port=5432;Database=newdemo262;Username=postgres;Password=Swa2sWA13;";
int IDZobsch { get; set; }
public Form4(int IDZ)
{
InitializeComponent();
IDZobsch = IDZ;
NpgsqlConnection npgsqlConnection = new NpgsqlConnection(ConnString);
npgsqlConnection.Open();
NpgsqlCommand npgsqlCommand = new NpgsqlCommand($@"SELECT ""Data_Z"", ""Data_P"", ""Adress_ID"", ""Status"", ""ID_Zakaza"" FROM public.""Zakaz"";", npgsqlConnection);
DataTable dataTable = new DataTable();
dataTable.Load(npgsqlCommand.ExecuteReader());
npgsqlConnection.Close();
maskedTextBox1.Text = dataTable.Rows[0].ItemArray[0].ToString();
maskedTextBox2.Text = dataTable.Rows[0].ItemArray[1].ToString();
comboBox1.SelectedIndex = int.Parse(dataTable.Rows[0].ItemArray[2].ToString());
comboBox2.SelectedIndex = int.Parse(dataTable.Rows[0].ItemArray[3].ToString());
NpgsqlConnection npgsqlConnection2 = new NpgsqlConnection(ConnString);
npgsqlConnection2.Open();
NpgsqlCommand npgsqlCommand2 = new NpgsqlCommand($@"SELECT (SELECT ""Articul"" FROM public.""Tovar"" WHERE ""ID_Tovar"" =""Tovar_ID""), ""Kolichestvo"" FROM public.""Sostav"" WHERE ""Zakaz_ID"" = {dataTable.Rows[0].ItemArray[4].ToString()};", npgsqlConnection2);
DataTable dataTable2 = new DataTable();
dataTable2.Load(npgsqlCommand2.ExecuteReader());
npgsqlConnection2.Close();
for (int j = 0; j < dataTable2.Rows.Count; j++)
{
listBox1.Items.Add(dataTable2.Rows[j].ItemArray[0].ToString() + " " + dataTable2.Rows[j].ItemArray[1].ToString());
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add( comboBox3.SelectedItem + " " + numericUpDown1.Value);
}
private void button3_Click(object sender, EventArgs e)
{
this.Hide();
Form3 form3 = new Form3();
form3.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
if (IDZobsch == 0)
{
NpgsqlConnection npgsqlConnection = new NpgsqlConnection(ConnString);
npgsqlConnection.Open();
NpgsqlCommand npgsqlCommand = new NpgsqlCommand($@"INSERT INTO public.""Zakaz""(""Data_Z"", ""Data_P"", ""Adress_ID"", ""Status"") VALUES ('{maskedTextBox1.Text}', '{maskedTextBox2.Text}', {comboBox1.SelectedIndex + 1}, {comboBox2.SelectedIndex + 1}) RETURNING ""ID_Zakaza"";", npgsqlConnection);
DataTable dataTable = new DataTable();
dataTable.Load(npgsqlCommand.ExecuteReader());
int IDZakaz = int.Parse(dataTable.Rows[0].ItemArray[0].ToString());
npgsqlConnection.Close();
int[] IDMnoga = new int[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
string rabota = listBox1.Items[i].ToString();
string[] razdeli = rabota.Split(' ');
NpgsqlConnection npgsqlConnection2 = new NpgsqlConnection(ConnString);
npgsqlConnection2.Open();
NpgsqlCommand npgsqlCommand2 = new NpgsqlCommand($@"INSERT INTO public.""Sostav""(""Zakaz_ID"", ""Tovar_ID"", ""Kolichestvo"") VALUES ({IDZakaz}, (SELECT ""ID_Tovar"" FROM public.""Tovar"" WHERE ""Articul"" = '{razdeli[0]}'), {razdeli[1]}) RETURNING ""ID_Sostav"";", npgsqlConnection2);
DataTable dataTable2 = new DataTable();
dataTable2.Load(npgsqlCommand2.ExecuteReader());
int IDZakaz2 = int.Parse(dataTable2.Rows[0].ItemArray[0].ToString());
IDMnoga[i] = IDZakaz2;
npgsqlConnection2.Close();
}
}
else
{
NpgsqlConnection npgsqlConnection = new NpgsqlConnection(ConnString);
npgsqlConnection.Open();
NpgsqlCommand npgsqlCommand = new NpgsqlCommand($@"UPDATE public.""Zakaz"" SET ""Data_Z""= '{maskedTextBox1.Text}', ""Data_P""= '{maskedTextBox2.Text}', ""Adress_ID""= {comboBox1.SelectedIndex + 1}, ""Status""= {comboBox2.SelectedIndex + 1} WHERE ""ID_Zakaza""= {IDZobsch};", npgsqlConnection);
npgsqlCommand.ExecuteNonQuery();
npgsqlConnection.Close();
NpgsqlConnection npgsqlConnection3 = new NpgsqlConnection(ConnString);
npgsqlConnection3.Open();
NpgsqlCommand npgsqlCommand3 = new NpgsqlCommand($@"DELETE FROM public.""Sostav"" WHERE ""Zakaz_ID"" = {IDZobsch};", npgsqlConnection3);
npgsqlCommand3.ExecuteNonQuery();
npgsqlConnection3.Close();
for (int i = 0; i < listBox1.Items.Count; i++)
{
string rabota = listBox1.Items[i].ToString();
string[] razdeli = rabota.Split(' ');
NpgsqlConnection npgsqlConnection2 = new NpgsqlConnection(ConnString);
npgsqlConnection2.Open();
NpgsqlCommand npgsqlCommand2 = new NpgsqlCommand($@"INSERT INTO public.""Sostav""(""Zakaz_ID"", ""Tovar_ID"", ""Kolichestvo"") VALUES ({IDZobsch}, (SELECT ""ID_Tovar"" FROM public.""Tovar"" WHERE ""Articul"" = '{razdeli[0]}'), {razdeli[1]}) RETURNING ""ID_Sostav"";", npgsqlConnection2);
DataTable dataTable2 = new DataTable();
dataTable2.Load(npgsqlCommand2.ExecuteReader());
int IDZakaz2 = int.Parse(dataTable2.Rows[0].ItemArray[0].ToString());
npgsqlConnection2.Close();
}
}
}
private void button4_Click(object sender, EventArgs e)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
private void button5_Click(object sender, EventArgs e)
{
NpgsqlConnection npgsqlConnection3 = new NpgsqlConnection(ConnString);
npgsqlConnection3.Open();
NpgsqlCommand npgsqlCommand3 = new NpgsqlCommand($@"DELETE FROM public.""Zakaz"" WHERE ""ID_Zakaza"" = {IDZobsch};", npgsqlConnection3);
npgsqlCommand3.ExecuteNonQuery();
npgsqlConnection3.Close();
}
}
}