LoginForm.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Windows.Forms;
  3. using Npgsql;
  4. using RestaurantApp.DAL;
  5. namespace RestaurantApp
  6. {
  7. public partial class LoginForm : Form
  8. {
  9. public LoginForm()
  10. {
  11. InitializeComponent();
  12. btnLogin.Click += BtnLogin_Click;
  13. btnCancel.Click += (s, e) => Application.Exit();
  14. }
  15. private void BtnLogin_Click(object sender, EventArgs e)
  16. {
  17. if (string.IsNullOrWhiteSpace(txtLogin.Text))
  18. {
  19. MessageBox.Show("Введите логин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  20. return;
  21. }
  22. try
  23. {
  24. using (var conn = Database.GetConnection())
  25. using (var cmd = new NpgsqlCommand(
  26. "SELECT full_name, role FROM app_user WHERE login = @login", conn))
  27. {
  28. cmd.Parameters.AddWithValue("login", txtLogin.Text);
  29. using (var r = cmd.ExecuteReader())
  30. {
  31. if (r.Read())
  32. {
  33. Program.CurrentUser = r.GetString(0);
  34. Program.CurrentRole = r.GetString(1);
  35. this.Hide();
  36. var mainForm = new MainForm();
  37. mainForm.Closed += (s, args) => this.Close();
  38. mainForm.Show();
  39. }
  40. else
  41. {
  42. MessageBox.Show("Неверный логин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  43. }
  44. }
  45. }
  46. }
  47. catch (Exception ex)
  48. {
  49. MessageBox.Show($"Ошибка подключения: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  50. }
  51. }
  52. }
  53. }