MainWindow.xaml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.Windows.Shapes;
  15. using System.Data.SqlClient;
  16. using System.Data;
  17. namespace SkladProject
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. }
  28. SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=praktika;Integrated Security=True");
  29. private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
  30. {
  31. DragMove();
  32. }
  33. private void Exit_Click(object sender, RoutedEventArgs e)
  34. {
  35. Application.Current.Shutdown();
  36. }
  37. private void WinMin_Click(object sender, RoutedEventArgs e)
  38. {
  39. this.WindowState = WindowState.Minimized;
  40. }
  41. private void logintxt_TextChanged(object sender, TextChangedEventArgs e)
  42. {
  43. if (sender is TextBox textBox)
  44. {
  45. textBox.Text = new string
  46. (textBox.Text.Where(ch => (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')).ToArray());
  47. }
  48. }
  49. private void passwordtxt_PasswordChanged(object sender, RoutedEventArgs e)
  50. {
  51. if (sender is TextBox textBox)
  52. {
  53. textBox.Text = new string
  54. (textBox.Text.Where(ch => (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')).ToArray());
  55. }
  56. }
  57. private void Vhod_Click(object sender, RoutedEventArgs e)
  58. {
  59. try
  60. {
  61. if (logintxt.Text == "" || passwordtxt.Password == "")
  62. {
  63. MessageBox.Show("Поля не могут быть пустыми!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  64. }
  65. else
  66. {
  67. con.Open();
  68. SqlCommand cmd = new SqlCommand("Select * from Users where Login ='" + logintxt.Text + "' and Password ='" + passwordtxt.Password + "'", con);
  69. cmd.CommandType = CommandType.Text;
  70. SqlDataAdapter adapter = new SqlDataAdapter();
  71. adapter.SelectCommand = cmd;
  72. DataSet dataSet = new DataSet();
  73. adapter.Fill(dataSet);
  74. if (dataSet.Tables[0].Rows.Count > 0)
  75. {
  76. string idrole = dataSet.Tables[0].Rows[0]["ID_RoleUser"].ToString();
  77. string username = dataSet.Tables[0].Rows[0]["ID_User"].ToString();
  78. if (idrole.ToString() == "1")
  79. {
  80. con.Close();
  81. WindowAdmin windowAdmin = new WindowAdmin();
  82. windowAdmin.Show();
  83. this.Close();
  84. }
  85. else
  86. {
  87. WorkerWindow workerWindow = new WorkerWindow();
  88. workerWindow.idusertxt.Text = username;
  89. con.Close();
  90. workerWindow.Show();
  91. this.Close();
  92. }
  93. }
  94. else
  95. {
  96. con.Close();
  97. MessageBox.Show("Неправильный логин/пароль!", "Вход", MessageBoxButton.OK, MessageBoxImage.Information);
  98. }
  99. }
  100. }
  101. catch (Exception ex)
  102. {
  103. con.Close();
  104. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  105. }
  106. }
  107. }
  108. }