addOtryad.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. namespace Дол_Восток
  12. {
  13. public partial class addOtryad : Form
  14. {
  15. SqlCommand query = null;
  16. public addOtryad()
  17. {
  18. InitializeComponent();
  19. }
  20. private void addOtryad_Load(object sender, EventArgs e)
  21. {
  22. try
  23. {
  24. query = new SqlCommand($"select surname + ' ' + [name] + ' ' + patronymic from sotrudnic", db_helper.sqlConnection);
  25. SqlDataReader reader = query.ExecuteReader();
  26. while (reader.Read())
  27. {
  28. cb_sotrudnic.Items.Add(reader.GetString(0));
  29. }
  30. reader.Close();
  31. query = new SqlCommand($"select korpus from korpus", db_helper.sqlConnection);
  32. reader = query.ExecuteReader();
  33. while (reader.Read())
  34. {
  35. cb_korpus.Items.Add(reader.GetString(0));
  36. }
  37. reader.Close();
  38. }
  39. catch
  40. {
  41. MessageBox.Show("Произошла непредвиденная ошибка!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  42. }
  43. }
  44. private void bt_add_Click(object sender, EventArgs e)
  45. {
  46. try
  47. {
  48. query = new SqlCommand($"select kod_sotrudnic from sotrudnic where (surname + ' ' + [name] + ' ' + patronymic) like '{cb_sotrudnic.SelectedItem}'", db_helper.sqlConnection);
  49. string kodSotrudnic = query.ExecuteScalar().ToString();
  50. query = new SqlCommand($"select kod_korpus from korpus where korpus like '{cb_korpus.SelectedItem}'", db_helper.sqlConnection);
  51. string kodKorpus = query.ExecuteScalar().ToString();
  52. query = new SqlCommand($"insert into otryad (kod_sotrudnic, kol_vo_children, kod_korpus) values (@kod_sotrudnic, @kol_vo_children, @kod_korpus)", db_helper.sqlConnection);
  53. query.Parameters.AddWithValue($"kod_sotrudnic", kodSotrudnic);
  54. query.Parameters.AddWithValue($"kol_vo_children", nud_kolChildren.Value);
  55. query.Parameters.AddWithValue($"kod_korpus", kodKorpus);
  56. if (query.ExecuteNonQuery().ToString().Equals("1"))
  57. {
  58. MessageBox.Show("Добавление прошло успешно!", "Уведомление", MessageBoxButtons.OK, MessageBoxIcon.Information);
  59. this.Close();
  60. }
  61. }
  62. catch
  63. {
  64. MessageBox.Show("Произошла непредвиденная ошибка!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  65. }
  66. }
  67. }
  68. }