DeleteRaspisaniye.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /// Логика взаимодействия для DeleteRaspisaniye.xaml
  20. /// </summary>
  21. public partial class DeleteRaspisaniye : Window
  22. {
  23. public DeleteRaspisaniye()
  24. {
  25. InitializeComponent();
  26. }
  27. private void Button_Click_Delete(object sender, RoutedEventArgs e)
  28. {
  29. DataTable sel = Select("select * from [dbo].[raspisaniye] where id = " + id_raspisaniye.Text);
  30. if (MessageBox.Show("Вы действительно хотите удалить данные ", "Удаление", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  31. {
  32. if(sel.Rows.Count > 0)
  33. {
  34. for (int i = 0; i < id_raspisaniye.Text.Length; i++)
  35. {
  36. if (id_raspisaniye.Text.Length > 0 && id_raspisaniye.Text[i] >= '0' && id_raspisaniye.Text[i] <= '9')
  37. {
  38. DataTable dt_raspisaniye = Delete("delete from [dbo].[raspisaniye] where [id] = " + id_raspisaniye.Text);
  39. MessageBox.Show("Занятие удалено");
  40. Raspisaniye a = new Raspisaniye();
  41. a.Show();
  42. Close();
  43. }
  44. else
  45. {
  46. MessageBox.Show("Поле код занятия введено не корректно");
  47. }
  48. }
  49. }
  50. else
  51. {
  52. MessageBox.Show("Данного кода не существует");
  53. }
  54. }
  55. }
  56. public DataTable Delete(string selectSQL)
  57. {
  58. DataTable dataTable = new DataTable("dataBase");
  59. SqlConnection sqlConnection = new SqlConnection("server=mssql; Trusted_Connection=YES;DataBase=gr692_mkm;");
  60. sqlConnection.Open();
  61. SqlCommand sqlCommand = sqlConnection.CreateCommand();
  62. sqlCommand.CommandText = selectSQL;
  63. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
  64. sqlDataAdapter.Fill(dataTable);
  65. sqlConnection.Close();
  66. return dataTable;
  67. }
  68. private void Button_Click_Back(object sender, RoutedEventArgs e)
  69. {
  70. Raspisaniye ras = new Raspisaniye();
  71. ras.Show();
  72. Close();
  73. }
  74. public DataTable Select(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. }