insertPatientCard.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 hospital
  12. {
  13. public partial class insertPatientCard : Form
  14. {
  15. SqlConnection sqlConnection = null;
  16. SqlCommand a = null;
  17. SqlDataReader reader = null;
  18. public insertPatientCard()
  19. {
  20. InitializeComponent();
  21. }
  22. private void add_Click(object sender, EventArgs e)
  23. {
  24. try
  25. {
  26. SqlCommand freeWardsFromDB = new SqlCommand($"select count(*) from wards where isfree like 1", sqlConnection);
  27. if (Convert.ToInt32(freeWardsFromDB.ExecuteScalar()) != 0)
  28. {
  29. DateTime date1 = DateTime.Today;
  30. SqlCommand cardID = new SqlCommand($"select id_card from medical_cards where (surname_patient + ' ' + name_patient + ' ' + midname_patient) like N'{patientCB.SelectedItem}'", sqlConnection);
  31. SqlCommand employeeID = new SqlCommand($"select id_employee from employees where (surname_employee + ' ' + name_employee + ' ' + midname_employee) like N'{employeeCB.SelectedItem}'", sqlConnection);
  32. SqlCommand diseaseID = new SqlCommand($"select id_disease from diseases where name_disease like N'{diseaseCB.SelectedItem}'", sqlConnection);
  33. SqlCommand severityID = new SqlCommand($"select id_severity from disease_severity where name_severity like N'{severityCB.SelectedItem}'", sqlConnection);
  34. int[] freeWards = new int[Convert.ToInt32(freeWardsFromDB.ExecuteScalar())];
  35. freeWardsFromDB = new SqlCommand($"select number_ward from wards where isfree like 1", sqlConnection);
  36. reader = freeWardsFromDB.ExecuteReader();
  37. int i = 0;
  38. while (reader.Read())
  39. {
  40. freeWards[i] = Convert.ToInt32(reader.GetString(0));
  41. i++;
  42. }
  43. reader.Close();
  44. Random rnd = new Random();
  45. int ward = freeWards[rnd.Next(0, freeWards.Length + 1)];
  46. a = new SqlCommand($"insert into [patientcards] ([id_card], [id_employee], [id_disease], [id_severity], [id_ward], [date_receipt]) values (@id_card, @id_employee, @id_disease, @id_severity, @id_ward, @date_receipt)", sqlConnection);
  47. a.Parameters.AddWithValue("id_card", cardID.ExecuteScalar().ToString());
  48. a.Parameters.AddWithValue("id_employee", employeeID.ExecuteScalar().ToString());
  49. a.Parameters.AddWithValue("id_disease", diseaseID.ExecuteScalar().ToString());
  50. a.Parameters.AddWithValue("id_severity", severityID.ExecuteScalar().ToString());
  51. a.Parameters.AddWithValue("id_ward", ward);
  52. a.Parameters.AddWithValue("date_receipt", $"{date1.Month}/{date1.Day}/{date1.Year}");
  53. Array.Resize(ref auth.data, 6);
  54. auth.data[0] = $"Пациент: {patientCB.SelectedItem}";
  55. auth.data[1] = $"Лечащий врач: {employeeCB.SelectedItem}";
  56. auth.data[2] = $"Болезнь: {diseaseCB.SelectedItem}";
  57. auth.data[3] = $"Тяжесть: {severityCB.SelectedItem}";
  58. auth.data[4] = $"Палата: {ward}";
  59. auth.data[5] = $"Дата записи: {date1.Month}/{date1.Day}/{date1.Year}";
  60. if (a.ExecuteNonQuery() == 1)
  61. {
  62. MessageBox.Show("Добавление данных произошло успешно", "Уведомление", MessageBoxButtons.OK, MessageBoxIcon.Information);
  63. debugAndTrace.writeInFile("Запись пациента в палату", auth.data);
  64. SqlCommand setWardOccupy = new SqlCommand($"update wards set isfree = 0 where number_ward like {ward}", sqlConnection);
  65. SqlCommand setIllPatient = new SqlCommand($"update medical_cards set isill = 1 where id_card like {cardID.ExecuteScalar()}", sqlConnection);
  66. setIllPatient.ExecuteNonQuery();
  67. setWardOccupy.ExecuteNonQuery();
  68. this.Close();
  69. }
  70. else
  71. {
  72. MessageBox.Show("Произошла ошибка", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  73. debugAndTrace.writeInFile("Ошибка добавления данных карты пациента", auth.data);
  74. }
  75. }
  76. else
  77. {
  78. debugAndTrace.writeInFile("Нет пустых палат для записи пациента");
  79. MessageBox.Show($"Пустых палат нет!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  80. }
  81. }
  82. catch
  83. {
  84. MessageBox.Show("Произошла ошибка", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  85. debugAndTrace.writeInFile("Ошибка добавления данных", auth.data);
  86. }
  87. }
  88. private void insertPatientCard_Load(object sender, EventArgs e)
  89. {
  90. try
  91. {
  92. FormCreating.openConnection(ref sqlConnection);
  93. a = new SqlCommand($"select surname_patient + ' ' + name_patient + ' ' + midname_patient from medical_cards where isill like 0", sqlConnection);
  94. reader = a.ExecuteReader();
  95. while (reader.Read())
  96. {
  97. patientCB.Items.Add(reader.GetString(0));
  98. }
  99. reader.Close();
  100. a = new SqlCommand($"select surname_employee + ' ' + name_employee + ' ' + midname_employee from employees", sqlConnection);
  101. reader = a.ExecuteReader();
  102. while (reader.Read())
  103. {
  104. employeeCB.Items.Add(reader.GetString(0));
  105. }
  106. reader.Close();
  107. a = new SqlCommand($"select name_disease from diseases", sqlConnection);
  108. reader = a.ExecuteReader();
  109. while (reader.Read())
  110. {
  111. diseaseCB.Items.Add(reader.GetString(0));
  112. }
  113. reader.Close();
  114. a = new SqlCommand($"select name_severity from disease_severity", sqlConnection);
  115. reader = a.ExecuteReader();
  116. while (reader.Read())
  117. {
  118. severityCB.Items.Add(reader.GetString(0));
  119. }
  120. reader.Close();
  121. }
  122. catch
  123. {
  124. MessageBox.Show("Произошла ошибка", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  125. debugAndTrace.writeInFile("Ошибка добавления данных", auth.data);
  126. }
  127. }
  128. }
  129. }