PolUpdateTrener.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /// Логика взаимодействия для PolUpdateTrener.xaml
  20. /// </summary>
  21. public partial class PolUpdateTrener : Window
  22. {
  23. public DataTable Select1(string selectSQL)
  24. {
  25. DataTable dataTable = new DataTable("dataBase");
  26. SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
  27. sqlConnection.Open();
  28. SqlCommand sqlCommand = sqlConnection.CreateCommand();
  29. sqlCommand.CommandText = selectSQL;
  30. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
  31. sqlDataAdapter.Fill(dataTable);
  32. sqlConnection.Close();
  33. return dataTable;
  34. }
  35. public DataTable Select(string selectSQL)
  36. {
  37. DataTable dataTable = new DataTable("dataBase");
  38. SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
  39. sqlConnection.Open();
  40. SqlCommand sqlCommand = sqlConnection.CreateCommand();
  41. sqlCommand.CommandText = selectSQL;
  42. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
  43. sqlDataAdapter.Fill(dataTable);
  44. sqlConnection.Close();
  45. return dataTable;
  46. }
  47. public DataTable Update(string selectSQL)
  48. {
  49. DataTable dataTable = new DataTable("dataBase");
  50. SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
  51. sqlConnection.Open();
  52. SqlCommand sqlCommand = sqlConnection.CreateCommand();
  53. sqlCommand.CommandText = selectSQL;
  54. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
  55. sqlDataAdapter.Fill(dataTable);
  56. sqlConnection.Close();
  57. return dataTable;
  58. }
  59. public PolUpdateTrener()
  60. {
  61. InitializeComponent();
  62. }
  63. private void Button_Click_Up(object sender, RoutedEventArgs e)
  64. {
  65. if (id_turik.Text.Length != 0 && Name.Text.Length != 0)
  66. {
  67. bool turik = false, imya = false;
  68. DataTable sel1 = Select1("select * from [dbo].[infotrener] where id = " + id_turik.Text);
  69. for (int i = 0; i < id_turik.Text.Length; i++)
  70. {
  71. if (turik)
  72. {
  73. turik = false;
  74. }
  75. if (id_turik.Text[i] >= '1' && id_turik.Text[i] <= '9')
  76. {
  77. if (sel1.Rows.Count > 0)
  78. {
  79. turik = true;
  80. }
  81. else
  82. {
  83. turik = false;
  84. }
  85. }
  86. if (turik == false)
  87. {
  88. MessageBox.Show("Поле код тренера должно содержать только цифры или вы ввели код, которого нет");
  89. }
  90. }
  91. DataTable sel = Select("select * from [dbo].[pol] where id = " + Name.Text);
  92. for (int i = 0; i < Name.Text.Length; i++)
  93. {
  94. if (imya)
  95. {
  96. imya = false;
  97. }
  98. if (Name.Text[i] == '1' || Name.Text[i] == '2' && Name.Text.Length == 1)
  99. {
  100. if (sel.Rows.Count > 0)
  101. {
  102. imya = true;
  103. }
  104. else
  105. {
  106. imya = false;
  107. }
  108. }
  109. if (imya == false)
  110. {
  111. MessageBox.Show("Поле пол должно содержать только код 1 (мужской) и 2 (женский)");
  112. break;
  113. }
  114. }
  115. if (turik && imya)
  116. {
  117. DataTable upname = Update("update [dbo].[infotrener] set fk_pol_id = '" + Name.Text + "' where id = " + id_turik.Text);
  118. MessageBox.Show("Поле пол для тренера " + id_turik.Text + " успешно изменено");
  119. ViborTrenerUpdate updateFamiliyaTrener = new ViborTrenerUpdate();
  120. updateFamiliyaTrener.Show();
  121. Close();
  122. }
  123. else
  124. {
  125. MessageBox.Show("Неправильный формат");
  126. }
  127. }
  128. else
  129. {
  130. MessageBox.Show("Все поля должны быть введены");
  131. }
  132. }
  133. private void Button_Click_Back(object sender, RoutedEventArgs e)
  134. {
  135. ViborTrenerUpdate viborTrenerUpdate = new ViborTrenerUpdate();
  136. viborTrenerUpdate.Show();
  137. Close();
  138. }
  139. }
  140. }