Class1.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Npgsql;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. using static System.Net.Mime.MediaTypeNames;
  9. namespace CarApp
  10. {
  11. public partial class LoginForm : Form
  12. {
  13. string conn = "Host=...";
  14. public string UserRole { get; private set; }
  15. TextBox txtLogin = new TextBox();
  16. TextBox txtPassword = new TextBox();
  17. Button btnLogin = new Button();
  18. public LoginForm()
  19. {
  20. Text = "Вход";
  21. txtLogin.Top = 20;
  22. txtLogin.Left = 20;
  23. txtLogin.Width = 200;
  24. txtPassword.Top = 60;
  25. txtPassword.Left = 20;
  26. txtPassword.Width = 200;
  27. txtPassword.PasswordChar = '*';
  28. btnLogin.Text = "Войти";
  29. btnLogin.Top = 100;
  30. btnLogin.Left = 20;
  31. btnLogin.Click += Login_Click;
  32. Controls.Add(txtLogin);
  33. Controls.Add(txtPassword);
  34. Controls.Add(btnLogin);
  35. }
  36. private void Login_Click(object sender, EventArgs e)
  37. {
  38. try
  39. {
  40. using var c = new NpgsqlConnection(conn);
  41. c.Open();
  42. var cmd = new NpgsqlCommand(
  43. "SELECT role FROM users WHERE login=@l AND password=@p", c);
  44. cmd.Parameters.AddWithValue("@l", txtLogin.Text);
  45. cmd.Parameters.AddWithValue("@p", txtPassword.Text);
  46. var role = cmd.ExecuteScalar();
  47. if (role != null)
  48. {
  49. UserRole = role.ToString();
  50. DialogResult = DialogResult.OK;
  51. Close();
  52. }
  53. else
  54. {
  55. MessageBox.Show("Неверный логин или пароль");
  56. }
  57. }
  58. catch (Exception ex)
  59. {
  60. MessageBox.Show(ex.Message);
  61. }
  62. }
  63. }
  64. }