insertPatientCard.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. public static string addPatient(string namePatient, string nameEmployee, string Disease, string Severity)
  23. {
  24. try
  25. {
  26. SqlConnection sqlConnection = null;
  27. SqlDataReader reader = null;
  28. SqlCommand a = null;
  29. FormCreating.openConnection(ref sqlConnection);
  30. SqlCommand freeWardsFromDB = new SqlCommand($"select count(*) from wards where isfree like 1", sqlConnection);
  31. if (Convert.ToInt32(freeWardsFromDB.ExecuteScalar()) != 0)
  32. {
  33. DateTime date1 = DateTime.Today;
  34. SqlCommand cardID = new SqlCommand($"select id_card from medical_cards where (surname_patient + ' ' + name_patient + ' ' + midname_patient) like N'{namePatient}'", sqlConnection);
  35. SqlCommand employeeID = new SqlCommand($"select id_employee from employees where (surname_employee + ' ' + name_employee + ' ' + midname_employee) like N'{nameEmployee}'", sqlConnection);
  36. SqlCommand diseaseID = new SqlCommand($"select id_disease from diseases where name_disease like N'{Disease}'", sqlConnection);
  37. SqlCommand severityID = new SqlCommand($"select id_severity from disease_severity where name_severity like N'{Severity}'", sqlConnection);
  38. int[] freeWards = new int[Convert.ToInt32(freeWardsFromDB.ExecuteScalar())];
  39. freeWardsFromDB = new SqlCommand($"select number_ward from wards where isfree like 1", sqlConnection);
  40. reader = freeWardsFromDB.ExecuteReader();
  41. int i = 0;
  42. while (reader.Read())
  43. {
  44. freeWards[i] = Convert.ToInt32(reader.GetString(0));
  45. i++;
  46. }
  47. reader.Close();
  48. Random rnd = new Random();
  49. int ward = freeWards[rnd.Next(0, freeWards.Length + 1)];
  50. 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);
  51. a.Parameters.AddWithValue("id_card", cardID.ExecuteScalar().ToString());
  52. a.Parameters.AddWithValue("id_employee", employeeID.ExecuteScalar().ToString());
  53. a.Parameters.AddWithValue("id_disease", diseaseID.ExecuteScalar().ToString());
  54. a.Parameters.AddWithValue("id_severity", severityID.ExecuteScalar().ToString());
  55. a.Parameters.AddWithValue("id_ward", ward);
  56. a.Parameters.AddWithValue("date_receipt", $"{date1.Month}/{date1.Day}/{date1.Year}");
  57. Array.Resize(ref auth.data, 6);
  58. auth.data[0] = $"Пациент: {namePatient}";
  59. auth.data[1] = $"Лечащий врач: {nameEmployee}";
  60. auth.data[2] = $"Болезнь: {Disease}";
  61. auth.data[3] = $"Тяжесть: {Severity}";
  62. auth.data[4] = $"Палата: {ward}";
  63. auth.data[5] = $"Дата записи: {date1.Month}/{date1.Day}/{date1.Year}";
  64. if (a.ExecuteNonQuery() == 1)
  65. {
  66. SqlCommand setWardOccupy = new SqlCommand($"update wards set isfree = 0 where number_ward like {ward}", sqlConnection);
  67. SqlCommand setIllPatient = new SqlCommand($"update medical_cards set isill = 1 where id_card like {cardID.ExecuteScalar()}", sqlConnection);
  68. setIllPatient.ExecuteNonQuery();
  69. setWardOccupy.ExecuteNonQuery();
  70. return "Добавление данных произошло успешно";
  71. }
  72. else
  73. {
  74. return "Произошла ошибка";
  75. }
  76. }
  77. else
  78. {
  79. return "Пустых палат нет!";
  80. }
  81. }
  82. catch
  83. {
  84. return "Произошла ошибка";
  85. }
  86. }
  87. private void add_Click(object sender, EventArgs e)
  88. {
  89. try
  90. {
  91. SqlCommand freeWardsFromDB = new SqlCommand($"select count(*) from wards where isfree like 1", sqlConnection);
  92. if (Convert.ToInt32(freeWardsFromDB.ExecuteScalar()) != 0)
  93. {
  94. DateTime date1 = DateTime.Today;
  95. SqlCommand cardID = new SqlCommand($"select id_card from medical_cards where (surname_patient + ' ' + name_patient + ' ' + midname_patient) like N'{patientCB.SelectedItem}'", sqlConnection);
  96. SqlCommand employeeID = new SqlCommand($"select id_employee from employees where (surname_employee + ' ' + name_employee + ' ' + midname_employee) like N'{employeeCB.SelectedItem}'", sqlConnection);
  97. SqlCommand diseaseID = new SqlCommand($"select id_disease from diseases where name_disease like N'{diseaseCB.SelectedItem}'", sqlConnection);
  98. SqlCommand severityID = new SqlCommand($"select id_severity from disease_severity where name_severity like N'{severityCB.SelectedItem}'", sqlConnection);
  99. int[] freeWards = new int[Convert.ToInt32(freeWardsFromDB.ExecuteScalar())];
  100. freeWardsFromDB = new SqlCommand($"select number_ward from wards where isfree like 1", sqlConnection);
  101. reader = freeWardsFromDB.ExecuteReader();
  102. int i = 0;
  103. while (reader.Read())
  104. {
  105. freeWards[i] = Convert.ToInt32(reader.GetString(0));
  106. i++;
  107. }
  108. reader.Close();
  109. Random rnd = new Random();
  110. int ward = freeWards[rnd.Next(0, freeWards.Length + 1)];
  111. 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);
  112. a.Parameters.AddWithValue("id_card", cardID.ExecuteScalar().ToString());
  113. a.Parameters.AddWithValue("id_employee", employeeID.ExecuteScalar().ToString());
  114. a.Parameters.AddWithValue("id_disease", diseaseID.ExecuteScalar().ToString());
  115. a.Parameters.AddWithValue("id_severity", severityID.ExecuteScalar().ToString());
  116. a.Parameters.AddWithValue("id_ward", ward);
  117. a.Parameters.AddWithValue("date_receipt", $"{date1.Month}/{date1.Day}/{date1.Year}");
  118. Array.Resize(ref auth.data, 6);
  119. auth.data[0] = $"Пациент: {patientCB.SelectedItem}";
  120. auth.data[1] = $"Лечащий врач: {employeeCB.SelectedItem}";
  121. auth.data[2] = $"Болезнь: {diseaseCB.SelectedItem}";
  122. auth.data[3] = $"Тяжесть: {severityCB.SelectedItem}";
  123. auth.data[4] = $"Палата: {ward}";
  124. auth.data[5] = $"Дата записи: {date1.Month}/{date1.Day}/{date1.Year}";
  125. if (a.ExecuteNonQuery() == 1)
  126. {
  127. MessageBox.Show("Добавление данных произошло успешно", "Уведомление", MessageBoxButtons.OK, MessageBoxIcon.Information);
  128. debugAndTrace.writeInFile("Запись пациента в палату", auth.data);
  129. SqlCommand setWardOccupy = new SqlCommand($"update wards set isfree = 0 where number_ward like {ward}", sqlConnection);
  130. SqlCommand setIllPatient = new SqlCommand($"update medical_cards set isill = 1 where id_card like {cardID.ExecuteScalar()}", sqlConnection);
  131. setIllPatient.ExecuteNonQuery();
  132. setWardOccupy.ExecuteNonQuery();
  133. this.Close();
  134. }
  135. else
  136. {
  137. MessageBox.Show("Произошла ошибка", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  138. debugAndTrace.writeInFile("Ошибка добавления данных карты пациента", auth.data);
  139. }
  140. }
  141. else
  142. {
  143. debugAndTrace.writeInFile("Нет пустых палат для записи пациента");
  144. MessageBox.Show($"Пустых палат нет!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  145. }
  146. }
  147. catch
  148. {
  149. MessageBox.Show("Произошла ошибка", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  150. debugAndTrace.writeInFile("Ошибка добавления данных", auth.data);
  151. }
  152. }
  153. private void insertPatientCard_Load(object sender, EventArgs e)
  154. {
  155. try
  156. {
  157. FormCreating.openConnection(ref sqlConnection);
  158. a = new SqlCommand($"select surname_patient + ' ' + name_patient + ' ' + midname_patient from medical_cards where isill like 0", sqlConnection);
  159. reader = a.ExecuteReader();
  160. while (reader.Read())
  161. {
  162. patientCB.Items.Add(reader.GetString(0));
  163. }
  164. reader.Close();
  165. a = new SqlCommand($"select surname_employee + ' ' + name_employee + ' ' + midname_employee from employees", sqlConnection);
  166. reader = a.ExecuteReader();
  167. while (reader.Read())
  168. {
  169. employeeCB.Items.Add(reader.GetString(0));
  170. }
  171. reader.Close();
  172. a = new SqlCommand($"select name_disease from diseases", sqlConnection);
  173. reader = a.ExecuteReader();
  174. while (reader.Read())
  175. {
  176. diseaseCB.Items.Add(reader.GetString(0));
  177. }
  178. reader.Close();
  179. a = new SqlCommand($"select name_severity from disease_severity", sqlConnection);
  180. reader = a.ExecuteReader();
  181. while (reader.Read())
  182. {
  183. severityCB.Items.Add(reader.GetString(0));
  184. }
  185. reader.Close();
  186. }
  187. catch
  188. {
  189. MessageBox.Show("Произошла ошибка", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  190. debugAndTrace.writeInFile("Ошибка добавления данных", auth.data);
  191. }
  192. }
  193. }
  194. }