using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SUBD { public partial class fAddUser : Form { public fAddUser() { InitializeComponent(); } private void bPhoto_Click(object sender, EventArgs e) { openFileDialog1.Filter = "Image files(*.png; *.jpg; *jpeg; *.bmp)| *.png; *.jpg; *jpeg; *.bmp"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { pbImage.Image = new Bitmap(openFileDialog1.FileName); } catch { MessageBox.Show("Невозможно открыть!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } private void bClear_Click(object sender, EventArgs e) { pbImage.Image = Properties.Resources.zag; } private void BAddUser_Click(object sender, EventArgs e) { Vis(tbFam, lErrorFam); Vis(tbName, lErrorName); Vis(tbOtch, lErrorOtch); VisTo(tbNaim, lNaim); VisTo(tbNumberHouse, lNumberHouse); VisPhoto(pbImage, lErrorPhoto); if (tbFam.Text != "" && tbName.Text != "" && tbOtch.Text != "" && tbNaim.Text != "" && tbNumberHouse.Text != "" && pbImage.Image != null) { string adr = cbUch.Text + " " + tbNaim.Text + ", " + "дом " + tbNumberHouse.Text; if (tbNumberKv.Text != "") { adr += ", кв " + tbNumberKv.Text; } SqlConnection con = new SqlConnection(fMain.txtCon); con.Open(); string txt = $@"insert into Teacher(FamTeacher, NameTeacher, OtchTeacher, Adr, Role, Pol) values (N'{tbFam.Text}', N'{tbName.Text}', N'{tbOtch.Text}', N'{adr}',N'{lstIdRole[cbRole.SelectedIndex]}',N'{cbPol.Text}')"; zapr1 = txt; SqlCommand com = new SqlCommand(txt, con); com.ExecuteNonQuery(); con.Close(); string idnewuser = ""; con.Open(); txt = "select * from Teacher"; com = new SqlCommand(txt, con); SqlDataReader rez = com.ExecuteReader(); while (rez.Read()) { idnewuser = rez["IdTeacher"].ToString(); } con.Close(); con.Open(); MemoryStream stream = new MemoryStream(); pbImage.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Png); NewPhoto = stream.ToArray(); byte[] masImages = stream.ToArray(); string txtqe = $@"update Teacher set Photo=@newphoto where idTeacher={idnewuser}"; zapr2 = txtqe; com = new SqlCommand(txtqe, con); com.Parameters.AddWithValue("@newphoto", NewPhoto); com.ExecuteNonQuery(); con.Close(); NewPhoto = null; MessageBox.Show("Выполено!", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning); MessageBox.Show($"Запрос {zapr1}\nЗапрос2 {zapr2}"); con.Close(); } else { MessageBox.Show("Введите все данные!", "Неполный ввод", MessageBoxButtons.OK, MessageBoxIcon.Stop); } } List lstIdRole = new List(); byte[] NewPhoto; void GetRole() { lstIdRole.Clear(); cbRole.Items.Clear(); SqlConnection con = new SqlConnection(fMain.txtCon); con.Open(); string txt = "select * from RoleTeacher"; SqlCommand com = new SqlCommand(txt, con); SqlDataReader rez = com.ExecuteReader(); while (rez.Read()) { lstIdRole.Add(rez["IdRole"].ToString()); cbRole.Items.Add(rez["NameRole"].ToString()); } con.Close(); } void Vis(TextBox tb, Label l) { if (tb.Text == "") l.Visible = true; else { l.Visible = false; } } void VisTo(TextBox tb, Label l) { if (tb.Text != "") { l.ForeColor = Color.Black; } else { l.ForeColor = Color.DarkRed; } } void VisPhoto(PictureBox pb, Label l) { if (pbImage.Image == null) { l.Visible = true; } else l.Visible = false; } string zapr1 = ""; string zapr2 = ""; private void fAddUser_Load(object sender, EventArgs e) { this.Icon = Properties.Resources.imgIco; pbLogo.Image = Properties.Resources.imgPng; GetRole(); cbUch.SelectedIndex = 0; cbRole.SelectedIndex = 0; cbPol.SelectedIndex = 0; } private void tbNumberKv_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar>='0' && e.KeyChar<='9')|| e.KeyChar == (char)Keys.Back) { e.Handled = false; } else { e.Handled = true; } } } }