MainWindow.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using System.Data.SqlClient;
  17. using System.Data;
  18. namespace HotelCalifornia
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для MainWindow.xaml
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. public MainWindow()
  26. {
  27. InitializeComponent();
  28. }
  29. //Строка подключения
  30. SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=kursah;Integrated Security=True");
  31. //Перетаскивание окна
  32. private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
  33. {
  34. try
  35. {
  36. DragMove();
  37. }
  38. catch
  39. {
  40. }
  41. }
  42. //Выход из приложения
  43. private void Close(object sender, RoutedEventArgs e)
  44. {
  45. Application.Current.Shutdown();
  46. }
  47. //Авторизация пользователя
  48. private void Vhod(object sender, RoutedEventArgs e)
  49. {
  50. try
  51. {
  52. if (logintxt.Text == "" || passwordtxt.Password == "")
  53. {
  54. MessageBox.Show("Поля не могут быть пустыми!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  55. }
  56. else
  57. {
  58. con.Open();
  59. SqlCommand cmd = new SqlCommand("Select * from Administrator where Login ='" + logintxt.Text + "' and Password ='" + passwordtxt.Password + "'", con);
  60. cmd.CommandType = CommandType.Text;
  61. SqlDataAdapter adapter = new SqlDataAdapter();
  62. adapter.SelectCommand = cmd;
  63. DataSet dataSet = new DataSet();
  64. adapter.Fill(dataSet);
  65. if (dataSet.Tables[0].Rows.Count > 0)
  66. {
  67. string idrole = dataSet.Tables[0].Rows[0]["ID_Role"].ToString();
  68. string username = dataSet.Tables[0].Rows[0]["ID_Administrator"].ToString();
  69. if (idrole.ToString() == "1")
  70. {
  71. Variant variant = new Variant();
  72. variant.idadmintxt.Text = username;
  73. con.Close();
  74. variant.Show();
  75. this.Close();
  76. }
  77. else
  78. {
  79. Staff staff = new Staff();
  80. staff.idadmintxt.Text = username;
  81. con.Close();
  82. staff.Show();
  83. this.Close();
  84. }
  85. }
  86. else
  87. {
  88. con.Close();
  89. logintxt.Text = "";
  90. passwordtxt.Password = "";
  91. MessageBox.Show("Такого Администратора нет в системе!", "Вход", MessageBoxButton.OK, MessageBoxImage.Information);
  92. }
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. con.Close();
  98. MessageBox.Show("Возникла ошибка! " + ex.ToString(),"Ошибка",MessageBoxButton.OK, MessageBoxImage.Error);
  99. }
  100. }
  101. //Ограничение для ввода текста
  102. #region Ограничения
  103. //Запрет пробела
  104. private void logintxt_PreviewKeyDown(object sender, KeyEventArgs e)
  105. {
  106. if (e.Key == Key.Space)
  107. {
  108. e.Handled = true;
  109. }
  110. }
  111. //Запрет пробела
  112. private void passwordtxt_PreviewKeyDown(object sender, KeyEventArgs e)
  113. {
  114. if (e.Key == Key.Space)
  115. {
  116. e.Handled = true;
  117. }
  118. }
  119. //Определенные символы
  120. private void logintxt_TextChanged(object sender, TextChangedEventArgs e)
  121. {
  122. if (sender is TextBox textBox)
  123. {
  124. textBox.Text = new string
  125. (textBox.Text.Where(ch => (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')).ToArray());
  126. }
  127. }
  128. //Определенные символы
  129. private void passwordtxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
  130. {
  131. bool a = new Regex("[^A-Z]+").IsMatch(e.Text);
  132. bool b = new Regex("[^a-z]+").IsMatch(e.Text);
  133. bool c = new Regex("[^0-9]+").IsMatch(e.Text);
  134. e.Handled = a && b && c;
  135. }
  136. #endregion
  137. //Свернуть окно
  138. private void WindMin_Click(object sender, RoutedEventArgs e)
  139. {
  140. this.WindowState = WindowState.Minimized;
  141. }
  142. }
  143. }