AddUchenik.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Shapes;
  16. namespace KursachMukha692gr
  17. {
  18. /// <summary>
  19. /// Логика взаимодействия для AddUchenik.xaml
  20. /// </summary>
  21. public partial class AddUchenik : Window
  22. {
  23. public AddUchenik()
  24. {
  25. InitializeComponent();
  26. }
  27. public DataTable Select(string selectSQL)
  28. {
  29. DataTable dataTable = new DataTable("dataBase");
  30. SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
  31. sqlConnection.Open();
  32. SqlCommand sqlCommand = sqlConnection.CreateCommand();
  33. sqlCommand.CommandText = selectSQL;
  34. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
  35. sqlDataAdapter.Fill(dataTable);
  36. sqlConnection.Close();
  37. return dataTable;
  38. }
  39. public DataTable Select1(string selectSQL)
  40. {
  41. DataTable dataTable = new DataTable("dataBase");
  42. SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
  43. sqlConnection.Open();
  44. SqlCommand sqlCommand = sqlConnection.CreateCommand();
  45. sqlCommand.CommandText = selectSQL;
  46. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
  47. sqlDataAdapter.Fill(dataTable);
  48. sqlConnection.Close();
  49. return dataTable;
  50. }
  51. public DataTable Select2(string selectSQL)
  52. {
  53. DataTable dataTable = new DataTable("dataBase");
  54. SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
  55. sqlConnection.Open();
  56. SqlCommand sqlCommand = sqlConnection.CreateCommand();
  57. sqlCommand.CommandText = selectSQL;
  58. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
  59. sqlDataAdapter.Fill(dataTable);
  60. sqlConnection.Close();
  61. return dataTable;
  62. }
  63. private void Button_Click_Add(object sender, RoutedEventArgs e)
  64. {
  65. if (familiyauch.Text != "" && imyauch.Text != "" && otchuch.Text != "" && datauch.Text != "" && numberuch.Text != "" && disciplina.Text != "" && pol_id.Text != "" && gruppa.Text != "")
  66. {
  67. bool fam = false, imya = false, otch = false, data = false, num = false;
  68. bool dis = false, pol = false, gr = false;
  69. for (int i = 0; i < familiyauch.Text.Length; i++)
  70. {
  71. if (fam)
  72. {
  73. fam = false;
  74. }
  75. if (familiyauch.Text[i] >= 'А' && familiyauch.Text[i] <= 'Я' || familiyauch.Text[i] >= 'а' && familiyauch.Text[i] <= 'я')
  76. {
  77. fam = true;
  78. break;
  79. }
  80. if (fam == false)
  81. {
  82. MessageBox.Show("Поле фамилия может содержать только русские символы");
  83. break;
  84. }
  85. }
  86. for (int i = 0; i < imyauch.Text.Length; i++)
  87. {
  88. if (imya)
  89. {
  90. imya = false;
  91. }
  92. if (imyauch.Text[i] >= 'А' && imyauch.Text[i] <= 'Я' || imyauch.Text[i] >= 'а' && imyauch.Text[i] <= 'я')
  93. {
  94. imya = true;
  95. break;
  96. }
  97. if (imya == false)
  98. {
  99. MessageBox.Show("Поле имя может содержать только русские символы");
  100. break;
  101. }
  102. }
  103. for (int i = 0; i < otchuch.Text.Length; i++)
  104. {
  105. if (otch)
  106. {
  107. otch = false;
  108. }
  109. if (otchuch.Text[i] >= 'А' && otchuch.Text[i] <= 'Я' || otchuch.Text[i] >= 'а' && otchuch.Text[i] <= 'я')
  110. {
  111. otch = true;
  112. break;
  113. }
  114. if (otch == false)
  115. {
  116. MessageBox.Show("Поле отчество может содержать только русские символы");
  117. break;
  118. }
  119. }
  120. for (int i = 0; i < datauch.Text.Length; i++)
  121. {
  122. if (data)
  123. {
  124. data = false;
  125. }
  126. if(datauch.Text.Length > 0)
  127. {
  128. data = true;
  129. break;
  130. }
  131. if (data == false)
  132. {
  133. MessageBox.Show("Поле дата рождения может содержать только цифры");
  134. break;
  135. }
  136. }
  137. for (int i = 0; i < numberuch.Text.Length; i++)//проверка номера телефона
  138. {
  139. if (num)
  140. {
  141. num = false;
  142. }
  143. if (numberuch.Text[i] >= '0' && numberuch.Text[i] <= '9' && numberuch.Text.Length == 11)
  144. {
  145. num = true;
  146. break;
  147. }
  148. if (num == false)
  149. {
  150. MessageBox.Show("Поле номер телефона может содержать только цифры (без знака + и 11 цифр)");
  151. break;
  152. }
  153. }
  154. DataTable sel = Select("select * from [dbo].[disciplina] where id = " + disciplina.Text);
  155. for (int i = 0; i < disciplina.Text.Length; i++)
  156. {
  157. if (dis)
  158. {
  159. dis = false;
  160. }
  161. if (disciplina.Text[i] >= '0' && disciplina.Text[i] <= '7' && disciplina.Text.Length == 1)
  162. {
  163. if (sel.Rows.Count > 0)
  164. {
  165. dis = true;
  166. }
  167. else
  168. {
  169. dis = false;
  170. }
  171. }
  172. if (dis == false)
  173. {
  174. MessageBox.Show("Поле дисциплина должно содержать код дисциплины (1-хоккей,2-Мини-футбол,3-Волейбол,4-Футбол,5-Баскетбол,6-Плавание,7-Бокс)");
  175. break;
  176. }
  177. }
  178. DataTable sel1 = Select1("select * from [dbo].[gruppa] where id = " + gruppa.Text);
  179. for (int i = 0; i < gruppa.Text.Length; i++)
  180. {
  181. if (gr)
  182. {
  183. gr = false;
  184. }
  185. if (gruppa.Text[i] >= '0' && gruppa.Text[i] <= '9' && gruppa.Text.Length == 3)
  186. {
  187. if (sel1.Rows.Count > 0)
  188. {
  189. gr = true;
  190. }
  191. else
  192. {
  193. gr = false;
  194. }
  195. }
  196. if (gr == false)
  197. {
  198. MessageBox.Show("Поле группа должно содержать цифры (3 цифры) или вы ввели группу, которой нет");
  199. break;
  200. }
  201. }
  202. DataTable sel2 = Select2("select * from [dbo].[pol] where id = " + pol_id.Text);
  203. for (int i = 0; i < pol_id.Text.Length; i++)
  204. {
  205. if (pol)
  206. {
  207. pol = false;
  208. }
  209. if (pol_id.Text[i] == '1' || pol_id.Text[i] == '2' && pol_id.Text.Length == 1)
  210. {
  211. if (sel2.Rows.Count > 0)
  212. {
  213. pol = true;
  214. }
  215. else
  216. {
  217. pol = false;
  218. }
  219. }
  220. if (pol == false)
  221. {
  222. MessageBox.Show("Поле пол должно содержать только цифры 1 (мужской) и 2 (женский)");
  223. break;
  224. }
  225. }
  226. if(fam && imya && otch && data && num && dis && pol && gr)
  227. {
  228. DataTable dt_infouchenik = Insert("insert into [dbo].[infouchenik] values " +
  229. "('" + familiyauch.Text + "','" + imyauch.Text + "','" + otchuch.Text +
  230. "','" + datauch.Text + "','" + disciplina.Text + "','" + gruppa.Text +
  231. "','" + pol_id.Text + "','" + numberuch.Text + "')");
  232. MessageBox.Show("Ученик добавлен");
  233. SpisokUchenikov mainreg = new SpisokUchenikov();
  234. mainreg.Show();
  235. Close();
  236. }
  237. else
  238. {
  239. MessageBox.Show("Неправильный формат");
  240. }
  241. }
  242. else
  243. {
  244. MessageBox.Show("Все строки должны быть заполнены");
  245. }
  246. }
  247. private void Button_Click_Back(object sender, RoutedEventArgs e)
  248. {
  249. SpisokUchenikov spisok = new SpisokUchenikov();
  250. spisok.Show();
  251. Close();
  252. }
  253. public DataTable Insert(string selectSQL)
  254. {
  255. DataTable dataTable = new DataTable("dataBase");
  256. SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
  257. sqlConnection.Open();
  258. SqlCommand sqlCommand = sqlConnection.CreateCommand();
  259. sqlCommand.CommandText = selectSQL;
  260. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
  261. sqlDataAdapter.Fill(dataTable);
  262. sqlConnection.Close();
  263. return dataTable;
  264. }
  265. }
  266. }