Auth.xaml.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.Shapes;
  14. using System.Data.SqlClient;
  15. using System.Data;
  16. using System.Configuration;
  17. namespace kursach.Windows
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для Auth.xaml
  21. /// </summary>
  22. public partial class Auth : Window
  23. {
  24. string connectionString;
  25. SqlDataAdapter adapter = new SqlDataAdapter();
  26. DataTable usersTable = new DataTable();
  27. public Auth()
  28. {
  29. InitializeComponent();
  30. //получаем строку подключения из app.config
  31. connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  32. }
  33. private void back(object sender, RoutedEventArgs e)
  34. {
  35. //вернуться назад
  36. MainWindow main = new MainWindow();
  37. main.Show();
  38. Windows.Menu menu = new Windows.Menu();
  39. menu.ShowDialog();
  40. Close();
  41. }
  42. private void reg(object sender, RoutedEventArgs e)
  43. {
  44. //перейти к регистрации
  45. Windows.Reg reg = new Windows.Reg();
  46. reg.Show();
  47. Close();
  48. }
  49. private void auth(object sender, RoutedEventArgs e)
  50. {
  51. //обработчик ошибок при авторизации
  52. if(login.Text == "" || password.Password == "")
  53. {
  54. MessageBox.Show("Ошибка! Пустые поля.");
  55. return;
  56. }
  57. SqlConnection connection = new SqlConnection(connectionString);
  58. connection.Open();
  59. SqlCommand command = new SqlCommand();
  60. command.CommandText = "SELECT * FROM Users WHERE Login = '" + login.Text + "' AND Password = '" + password.Password + "'";
  61. command.Connection = connection;
  62. adapter.SelectCommand = command;
  63. adapter.Fill(usersTable);
  64. if(usersTable.Rows.Count != 0)
  65. {
  66. //успех, переход в профиль
  67. MessageBox.Show("Авторизация прошла успешно.");
  68. Windows.Account acc = new Windows.Account(Convert.ToInt32(usersTable.Rows[0][0]));
  69. acc.Show();
  70. Close();
  71. }
  72. else
  73. {
  74. MessageBox.Show("Ошибка! Неверный логин и/или пароль.");
  75. return;
  76. }
  77. connection.Close();
  78. }
  79. }
  80. }