fAdd.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_Medical_Staff
  13. {
  14. public partial class fAdd : Form
  15. {
  16. public static string txtCon = @"Data Source=DESKTOP-5VOE8QR\SQLEXPRESS;Initial Catalog=Personnel;Integrated Security=True";
  17. public fAdd()
  18. {
  19. InitializeComponent();
  20. }
  21. List<string> lstId = new List<string>();
  22. byte[] NewPhoto;
  23. void GetRole()
  24. {
  25. lstId.Clear();
  26. cbDolj.Items.Clear();
  27. SqlConnection con = new SqlConnection(Form1.txtCon);
  28. con.Open();
  29. string txt = "select * from Roles";
  30. SqlCommand com = new SqlCommand(txt, con);
  31. SqlDataReader rez = com.ExecuteReader();
  32. while (rez.Read())
  33. {
  34. lstId.Add(rez["IDRole"].ToString());
  35. cbDolj.Items.Add(rez["Role"].ToString());
  36. }
  37. con.Close();
  38. }
  39. private void fAdd_Load(object sender, EventArgs e)
  40. {
  41. GetRole();
  42. }
  43. string zapr1 = "";
  44. string zapr2 = "";
  45. private void bOk_Click(object sender, EventArgs e)
  46. {
  47. if (tbFam.Text != "" && tbName.Text != "" && tbOtch.Text != "" && tbPol.Text != "" && tbPhone.Text != "" && cbDolj.Text != "" )
  48. {
  49. SqlConnection con = new SqlConnection(txtCon);
  50. con.Open();
  51. string txt = $@"insert into Medical_staff(Name, Surname, Otch, Gender, Telephone, IDRole)
  52. values (N'{tbName.Text}', N'{tbFam.Text}', N'{tbOtch.Text}',N'{tbPol.Text}',N'{tbPhone.Text}',N'{lstId[cbDolj.SelectedIndex]}')";
  53. SqlCommand com = new SqlCommand(txt, con);
  54. com.ExecuteNonQuery();
  55. con.Close();
  56. string idnewuser = "";
  57. con.Open();
  58. txt = "select * from Medical_staff";
  59. com = new SqlCommand(txt, con);
  60. SqlDataReader rez = com.ExecuteReader();
  61. while (rez.Read())
  62. {
  63. idnewuser = rez["IDHuman"].ToString();
  64. }
  65. con.Close();
  66. con.Open();
  67. MemoryStream stream = new MemoryStream();
  68. pbImage.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
  69. NewPhoto = stream.ToArray();
  70. byte[] masImages = stream.ToArray();
  71. string txtqe = $@"update Medical_staff
  72. set Photo=@newphoto
  73. where IDHuman={idnewuser}";
  74. zapr2 = txtqe;
  75. com = new SqlCommand(txtqe, con);
  76. com.Parameters.AddWithValue("@newphoto", NewPhoto);
  77. com.ExecuteNonQuery();
  78. con.Close();
  79. NewPhoto = null;
  80. MessageBox.Show("Выполено!", "Внимание",
  81. MessageBoxButtons.OK, MessageBoxIcon.Warning);
  82. MessageBox.Show($"Запрос {zapr1}\nЗапрос2 {zapr2}");
  83. con.Close();
  84. }
  85. else
  86. {
  87. MessageBox.Show("Вы не ввели все данные");
  88. }
  89. }
  90. private void bCancel_Click(object sender, EventArgs e)
  91. {
  92. this.Hide();
  93. }
  94. private void button2_Click(object sender, EventArgs e)
  95. {
  96. pbImage.Image = Properties.Resources.pngwing_com;
  97. }
  98. private void button1_Click(object sender, EventArgs e)
  99. {
  100. openFileDialog1.Filter = "Image files(*.png; *.jpg; *jpeg; *.bmp)| *.png; *.jpg; *jpeg; *.bmp";
  101. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  102. {
  103. try
  104. {
  105. pbImage.Image = new Bitmap(openFileDialog1.FileName);
  106. }
  107. catch
  108. {
  109. MessageBox.Show("Невозможно открыть!", "Внимание",
  110. MessageBoxButtons.OK, MessageBoxIcon.Warning);
  111. }
  112. }
  113. }
  114. }
  115. }