DeleteSpisokUchenikov.xaml.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /// Логика взаимодействия для DeleteSpisokUchenikov.xaml
  20. /// </summary>
  21. public partial class DeleteSpisokUchenikov : Window
  22. {
  23. public DeleteSpisokUchenikov()
  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. private void Button_Click_Delete(object sender, RoutedEventArgs e)
  40. {
  41. DataTable sel = Select("select * from [dbo].[infouchenik] where id = " + id_uch.Text);
  42. if (MessageBox.Show("Вы действительно хотите удалить данные ", "Удаление", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  43. {
  44. if(sel.Rows.Count > 0)
  45. {
  46. for (int i = 0; i < id_uch.Text.Length; i++)
  47. {
  48. if (id_uch.Text.Length > 0 && id_uch.Text[i] >= '0' && id_uch.Text[i] <= '9')
  49. {
  50. DataTable dt_uch = Delete("delete from [dbo].[infouchenik] where [id] = " + id_uch.Text);
  51. MessageBox.Show("Ученик удален");
  52. SpisokUchenikov uch = new SpisokUchenikov();
  53. uch.Show();
  54. Close();
  55. }
  56. else
  57. {
  58. MessageBox.Show("Поле код ученика введено не корректно");
  59. }
  60. }
  61. }
  62. else
  63. {
  64. MessageBox.Show("Данного кода не существует");
  65. }
  66. }
  67. }
  68. private void Button_Click_Back(object sender, RoutedEventArgs e)
  69. {
  70. SpisokUchenikov spisok = new SpisokUchenikov();
  71. spisok.Show();
  72. Close();
  73. }
  74. public DataTable Delete(string selectSQL)
  75. {
  76. DataTable dataTable = new DataTable("dataBase");
  77. SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
  78. sqlConnection.Open();
  79. SqlCommand sqlCommand = sqlConnection.CreateCommand();
  80. sqlCommand.CommandText = selectSQL;
  81. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
  82. sqlDataAdapter.Fill(dataTable);
  83. sqlConnection.Close();
  84. return dataTable;
  85. }
  86. }
  87. }