using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static СУБД_Фабрика.FormGetMaterial; namespace СУБД_Фабрика { public partial class FormGetMaterial : Form { public FormGetMaterial() { InitializeComponent(); } public struct Tkan { public string TitleTkan, ColorTkan, WidthTkan, LengthTkan, Comment; public Image PhotoTkan; } public struct Furn { public string TitleFurn, CountFurn; public Image PhotoFurn; } SqlConnection Con = new SqlConnection(FormMain.txtCon); List LstTkan = new List(); List LstFurn = new List(); private void BtnLoadPhoto_Click(object sender, EventArgs e) { try { if (openFileDialog1.ShowDialog() == DialogResult.OK) PbxPhotoTkan.Image = Image.FromFile(openFileDialog1.FileName); } catch { } } private void BtnClearPhoto_Click(object sender, EventArgs e) { if (MessageBox.Show("Очистить фотографию?", "Внимание", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) PbxPhotoTkan.Image = null; } private void BtnAddTkan_Click(object sender, EventArgs e) { Tkan Tk1 = new Tkan(); Tk1.TitleTkan = TbxTitleTkan.Text; Tk1.ColorTkan = TbxColorTkan.Text; Tk1.WidthTkan = TbxWidthTkan.Text; Tk1.LengthTkan = TbxLengthTkan.Text; Tk1.Comment = TbxCommentTkan.Text; Tk1.PhotoTkan = PbxPhotoTkan.Image; LstTkan.Add(Tk1); DgvTkan.Rows.Add(Tk1.TitleTkan, Tk1.ColorTkan, Tk1.WidthTkan, Tk1.LengthTkan, Tk1.Comment); } private void BtnDelTkan_Click(object sender, EventArgs e) { int NumTkan = DgvTkan.CurrentRow.Index; if (NumTkan >= 0) { if (MessageBox.Show("Удалить выделенную ткань?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { LstTkan.RemoveAt(NumTkan); DgvTkan.Rows.RemoveAt(NumTkan); } } else MessageBox.Show("Ткань не выделена", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question); } private void BtnOK_Click(object sender, EventArgs e) { foreach (Tkan tkan in LstTkan) { Con.Open(); if (tkan.PhotoTkan != null) // если есть изображение { string PhotoName = Guid.NewGuid().ToString() + ".jpg"; string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Application.StartupPath + "\\Clothes", PhotoName); string txtQuery = $@"insert into Cloth(Title, Color, Width, Length, Comment, Photo) values('{tkan.TitleTkan}','{tkan.ColorTkan}',{tkan.WidthTkan},{tkan.LengthTkan},'{tkan.Comment}','{PhotoName}')"; SqlCommand query = new SqlCommand(txtQuery, Con); query.ExecuteNonQuery(); tkan.PhotoTkan.Save(fileName, ImageFormat.Jpeg); } else { string txtQuery = $@"insert into Cloth(Title, Color, Width, Length, Comment) values('{tkan.TitleTkan}','{tkan.ColorTkan}',{tkan.WidthTkan},{tkan.LengthTkan},'{tkan.Comment}')"; SqlCommand query = new SqlCommand(txtQuery, Con); query.ExecuteNonQuery(); } Con.Close(); } foreach(Furn frn in LstFurn) { Con.Open(); if(frn.PhotoFurn != null) { string PhotoName = Guid.NewGuid().ToString() + ".jpg"; string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Application.StartupPath + "\\Clothes", PhotoName); string txtQuery = $@"insert into Furnitura(namefur, countfur, photo) values('{frn.TitleFurn}',{frn.CountFurn},'{PhotoName}')"; SqlCommand query = new SqlCommand(txtQuery, Con); query.ExecuteNonQuery(); frn.PhotoFurn.Save(fileName, ImageFormat.Jpeg); } else { string txtQuery = $@"insert into Furnitura(namefur, countfur) values('{frn.TitleFurn}',{frn.CountFurn})"; SqlCommand query = new SqlCommand(txtQuery, Con); query.ExecuteNonQuery(); } Con.Close(); } MessageBox.Show("Документ принят к учету.", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } private void BtnSavePhotoFurn_Click(object sender, EventArgs e) { try { if (openFileDialog1.ShowDialog() == DialogResult.OK) PbxPhotoFurn.Image = Image.FromFile(openFileDialog1.FileName); } catch { } } private void BtnClearPhotoFurn_Click(object sender, EventArgs e) { if (MessageBox.Show("Очистить фотографию?", "Внимание", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) PbxPhotoFurn.Image = null; } private void BtnAddFur_Click(object sender, EventArgs e) { Furn frn = new Furn(); frn.TitleFurn = TbxTitleFur.Text; frn.CountFurn = TbxCountFur.Text; frn.PhotoFurn = PbxPhotoFurn.Image; LstFurn.Add(frn); DgvFurn.Rows.Add(frn.TitleFurn, frn.CountFurn); } private void BtnDelFur_Click(object sender, EventArgs e) { int NumFurn = DgvFurn.CurrentRow.Index; if (NumFurn >= 0) { if (MessageBox.Show("Удалить выделенную фурнитуру?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { LstFurn.RemoveAt(NumFurn); DgvFurn.Rows.RemoveAt(NumFurn); } } else MessageBox.Show("Фурнитура не выделена", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question); } } }