fAddUser.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace SUBD
  13. {
  14. public partial class fAddUser : Form
  15. {
  16. public fAddUser()
  17. {
  18. InitializeComponent();
  19. }
  20. private void bPhoto_Click(object sender, EventArgs e)
  21. {
  22. openFileDialog1.Filter = "Image files(*.png; *.jpg; *jpeg; *.bmp)| *.png; *.jpg; *jpeg; *.bmp";
  23. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  24. {
  25. try
  26. {
  27. pbImage.Image = new Bitmap(openFileDialog1.FileName);
  28. }
  29. catch
  30. {
  31. MessageBox.Show("Невозможно открыть!", "Внимание",
  32. MessageBoxButtons.OK, MessageBoxIcon.Warning);
  33. }
  34. }
  35. }
  36. private void bClear_Click(object sender, EventArgs e)
  37. {
  38. pbImage.Image = Properties.Resources.zag;
  39. }
  40. private void BAddUser_Click(object sender, EventArgs e)
  41. {
  42. Vis(tbFam, lErrorFam);
  43. Vis(tbName, lErrorName);
  44. Vis(tbOtch, lErrorOtch);
  45. VisTo(tbNaim, lNaim);
  46. VisTo(tbNumberHouse, lNumberHouse);
  47. VisPhoto(pbImage, lErrorPhoto);
  48. if (tbFam.Text != "" && tbName.Text != "" && tbOtch.Text != "" && tbNaim.Text != "" && tbNumberHouse.Text != "" && pbImage.Image != null)
  49. {
  50. string adr = cbUch.Text + " " + tbNaim.Text + ", " + "дом " + tbNumberHouse.Text;
  51. if (tbNumberKv.Text != "")
  52. {
  53. adr += ", кв " + tbNumberKv.Text;
  54. }
  55. SqlConnection con = new SqlConnection(fMain.txtCon);
  56. con.Open();
  57. string txt = $@"insert into Teacher(FamTeacher, NameTeacher, OtchTeacher, Adr, Role, Pol)
  58. values (N'{tbFam.Text}', N'{tbName.Text}', N'{tbOtch.Text}', N'{adr}',N'{lstIdRole[cbRole.SelectedIndex]}',N'{cbPol.Text}')";
  59. zapr1 = txt;
  60. SqlCommand com = new SqlCommand(txt, con);
  61. com.ExecuteNonQuery();
  62. con.Close();
  63. string idnewuser = "";
  64. con.Open();
  65. txt = "select * from Teacher";
  66. com = new SqlCommand(txt, con);
  67. SqlDataReader rez = com.ExecuteReader();
  68. while (rez.Read())
  69. {
  70. idnewuser = rez["IdTeacher"].ToString();
  71. }
  72. con.Close();
  73. con.Open();
  74. MemoryStream stream = new MemoryStream();
  75. pbImage.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  76. NewPhoto = stream.ToArray();
  77. byte[] masImages = stream.ToArray();
  78. string txtqe = $@"update Teacher
  79. set Photo=@newphoto
  80. where idTeacher={idnewuser}";
  81. zapr2 = txtqe;
  82. com = new SqlCommand(txtqe, con);
  83. com.Parameters.AddWithValue("@newphoto", NewPhoto);
  84. com.ExecuteNonQuery();
  85. con.Close();
  86. NewPhoto = null;
  87. MessageBox.Show("Выполено!", "Внимание",
  88. MessageBoxButtons.OK, MessageBoxIcon.Warning);
  89. MessageBox.Show($"Запрос {zapr1}\nЗапрос2 {zapr2}");
  90. con.Close();
  91. }
  92. else
  93. {
  94. MessageBox.Show("Введите все данные!", "Неполный ввод", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  95. }
  96. }
  97. List<string> lstIdRole = new List<string>();
  98. byte[] NewPhoto;
  99. void GetRole()
  100. {
  101. lstIdRole.Clear();
  102. cbRole.Items.Clear();
  103. SqlConnection con = new SqlConnection(fMain.txtCon);
  104. con.Open();
  105. string txt = "select * from RoleTeacher";
  106. SqlCommand com = new SqlCommand(txt, con);
  107. SqlDataReader rez = com.ExecuteReader();
  108. while (rez.Read())
  109. {
  110. lstIdRole.Add(rez["IdRole"].ToString());
  111. cbRole.Items.Add(rez["NameRole"].ToString());
  112. }
  113. con.Close();
  114. }
  115. void Vis(TextBox tb, Label l)
  116. {
  117. if (tb.Text == "")
  118. l.Visible = true;
  119. else
  120. {
  121. l.Visible = false;
  122. }
  123. }
  124. void VisTo(TextBox tb, Label l)
  125. {
  126. if (tb.Text != "")
  127. {
  128. l.ForeColor = Color.Black;
  129. }
  130. else
  131. {
  132. l.ForeColor = Color.DarkRed;
  133. }
  134. }
  135. void VisPhoto(PictureBox pb, Label l)
  136. {
  137. if (pbImage.Image == null)
  138. {
  139. l.Visible = true;
  140. }
  141. else l.Visible = false;
  142. }
  143. string zapr1 = "";
  144. string zapr2 = "";
  145. private void fAddUser_Load(object sender, EventArgs e)
  146. {
  147. this.Icon = Properties.Resources.imgIco;
  148. pbLogo.Image = Properties.Resources.imgPng;
  149. GetRole();
  150. cbUch.SelectedIndex = 0;
  151. cbRole.SelectedIndex = 0;
  152. cbPol.SelectedIndex = 0;
  153. }
  154. private void tbNumberKv_KeyPress(object sender, KeyPressEventArgs e)
  155. {
  156. if ((e.KeyChar>='0' && e.KeyChar<='9')|| e.KeyChar == (char)Keys.Back)
  157. {
  158. e.Handled = false;
  159. }
  160. else
  161. {
  162. e.Handled = true;
  163. }
  164. }
  165. }
  166. }