MainWindow.xaml.cs 4.5 KB

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