demMalyhinMerkulov_shoe_store/LoginForm.cs
2025-11-26 11:52:04 +04:00

173 lines
6.3 KiB
C#

using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using System.Diagnostics.Eventing.Reader;
namespace demMalyhin
{
public partial class LoginForm : Form
{
public static string ClientSurname { get; private set; }
public static string ClientName { get; private set; }
public static string ClientPatronymic { get; private set; }
public LoginForm()
{
InitializeComponent();
loginField.KeyPress += TextBox_KeyPress_NoSpaces;
passField.KeyPress += TextBox_KeyPress_NoSpaces;
}
private void TextBox_KeyPress_NoSpaces(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ' ')
{
e.Handled = true;
}
}
private void label2_Click(object sender, EventArgs e)
{
}
private void ButtonLogin_Click(object sender, EventArgs e)
{
string loginUser = loginField.Text;
string passUser = passField.Text;
if (string.IsNullOrWhiteSpace(loginField.Text))
{
MessageBox.Show("Введите логин.", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (string.IsNullOrWhiteSpace(passField.Text))
{
MessageBox.Show("Введите пароль.", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
DB db = new DB();
using (MySqlConnection connection = db.getConnection())
{
DataTable table = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand command = new MySqlCommand("SELECT RoleID, login, Surname, Name, Patronymic FROM users WHERE login = @uL AND password = @uP", connection);
command.Parameters.Add("@uL", MySqlDbType.VarChar).Value = loginUser;
command.Parameters.Add("@uP", MySqlDbType.VarChar).Value = passUser;
adapter.SelectCommand = command;
try
{
connection.Open();
adapter.Fill(table);
if (table.Rows.Count == 1)
{
string roleID = table.Rows[0]["RoleID"].ToString();
string userLogin = table.Rows[0]["login"].ToString();
ClientSurname = Convert.ToString(table.Rows[0]["Surname"]);
ClientName = Convert.ToString(table.Rows[0]["Name"]);
ClientPatronymic = Convert.ToString(table.Rows[0]["Patronymic"]);
switch (roleID)
{
case "1":
MessageBox.Show("Вы авторизовались под ролью: Admin. Ваше фио: " + ClientSurname + " " + ClientName + " " + ClientPatronymic);
this.Hide();
AdmMainForm adm = new AdmMainForm();
adm.Show();
break;
case "2":
MessageBox.Show("Авторизация успешна, добро пожаловать! Вы авторизовались под ролью: Manager");
this.Hide();
ManagerMainForm usManager = new ManagerMainForm();
usManager.Show();
break;
case "3":
MessageBox.Show("Авторизация успешна, добро пожаловать! Вы авторизовались под ролью: Authorized Client");
this.Hide();
UserMainForm usClient = new UserMainForm();
usClient.Show();
break;
}
}
else
{
MessageBox.Show("Введен неверный логин или пароль", "Ошибка авторизации", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (MySqlException ex)
{
MessageBox.Show("Ошибка подключения к базе данных или выполнения запроса: " + ex.Message, "Ошибка БД", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
db.closeConnection();
}
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void regLabel_Click(object sender, EventArgs e)
{
this.Hide();
ManagerMainForm regForm = new ManagerMainForm();
regForm.Show();
}
private void closeBtn_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void checkBoxPass_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxPass.Checked)
{
passField.UseSystemPasswordChar = false;
}
else
{
passField.UseSystemPasswordChar = true;
}
}
private void label1_Click(object sender, EventArgs e)
{
this.Hide();
GuestMainForm guest = new GuestMainForm();
guest.Show();
}
private void LoginForm_Load(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.Icon1;
}
}
}