MainWindow.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. DragMove();
  35. }
  36. //Выход из приложения
  37. private void Close(object sender, RoutedEventArgs e)
  38. {
  39. Application.Current.Shutdown();
  40. }
  41. //Авторизация пользователя
  42. private void Vhod(object sender, RoutedEventArgs e)
  43. {
  44. try
  45. {
  46. if (logintxt.Text == "" || passwordtxt.Password == "")
  47. {
  48. MessageBox.Show("Поля не могут быть пустыми!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  49. }
  50. else
  51. {
  52. con.Open();
  53. SqlCommand cmd = new SqlCommand("Select * from Administrator where Login ='" + logintxt.Text + "' and Password ='" + passwordtxt.Password + "'", con);
  54. cmd.CommandType = CommandType.Text;
  55. SqlDataAdapter adapter = new SqlDataAdapter();
  56. adapter.SelectCommand = cmd;
  57. DataSet dataSet = new DataSet();
  58. adapter.Fill(dataSet);
  59. if (dataSet.Tables[0].Rows.Count > 0)
  60. {
  61. Variant variant = new Variant();
  62. string username = dataSet.Tables[0].Rows[0]["ID_Administrator"].ToString();
  63. variant.idadmintxt.Text = username;
  64. con.Close();
  65. variant.Show();
  66. this.Close();
  67. }
  68. else
  69. {
  70. con.Close();
  71. MessageBox.Show("Такого Администратора нет в системе!", "Вход", MessageBoxButton.OK, MessageBoxImage.Information);
  72. }
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. con.Close();
  78. MessageBox.Show("Возникла ошибка! " + ex.ToString(),"Ошибка",MessageBoxButton.OK, MessageBoxImage.Error);
  79. }
  80. }
  81. //Ограничение для ввода текста
  82. #region Ограничения
  83. //Запрет пробела
  84. private void logintxt_PreviewKeyDown(object sender, KeyEventArgs e)
  85. {
  86. if (e.Key == Key.Space)
  87. {
  88. e.Handled = true;
  89. }
  90. }
  91. //Запрет пробела
  92. private void passwordtxt_PreviewKeyDown(object sender, KeyEventArgs e)
  93. {
  94. if (e.Key == Key.Space)
  95. {
  96. e.Handled = true;
  97. }
  98. }
  99. //Определенные символы
  100. private void logintxt_TextChanged(object sender, TextChangedEventArgs e)
  101. {
  102. if (sender is TextBox textBox)
  103. {
  104. textBox.Text = new string
  105. (textBox.Text.Where(ch => (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')).ToArray());
  106. }
  107. }
  108. //Определенные символы
  109. private void passwordtxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
  110. {
  111. bool a = new Regex("[^A-Z]+").IsMatch(e.Text);
  112. bool b = new Regex("[^a-z]+").IsMatch(e.Text);
  113. bool c = new Regex("[^0-9]+").IsMatch(e.Text);
  114. e.Handled = a && b && c;
  115. }
  116. #endregion
  117. //Свернуть окно
  118. private void WindMin_Click(object sender, RoutedEventArgs e)
  119. {
  120. this.WindowState = WindowState.Minimized;
  121. }
  122. }
  123. }