Загрузка данных


using System;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace YourAppName
{
    public class LoginForm : Form
    {
        // Элементы интерфейса
        private TextBox txtLogin;
        private TextBox txtPassword;
        private Button btnLogin;
        private Button btnGuest;
        private Label lblLogin;
        private Label lblPassword;

        // ВАЖНО: Ваша строка подключения
        private string connectionString = @"Data Source=ВАШ_СЕРВЕР;Initial Catalog=ВАША_БД;Integrated Security=True";

        public LoginForm()
        {
            // Настройка самого окна
            this.Text = "Авторизация";
            this.Size = new Size(350, 250);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;

            // Инициализация элементов
            lblLogin = new Label() { Text = "Логин:", Location = new Point(30, 30), AutoSize = true };
            txtLogin = new TextBox() { Location = new Point(100, 27), Width = 180 };

            lblPassword = new Label() { Text = "Пароль:", Location = new Point(30, 70), AutoSize = true };
            txtPassword = new TextBox() { Location = new Point(100, 67), Width = 180, PasswordChar = '*' };

            btnLogin = new Button() { Text = "Войти", Location = new Point(100, 110), Width = 180, Height = 30 };
            btnGuest = new Button() { Text = "Войти как гость", Location = new Point(100, 150), Width = 180, Height = 30 };

            // Привязка событий клика
            btnLogin.Click += BtnLogin_Click;
            btnGuest.Click += BtnGuest_Click;

            // Добавление элементов на форму
            this.Controls.Add(lblLogin);
            this.Controls.Add(txtLogin);
            this.Controls.Add(lblPassword);
            this.Controls.Add(txtPassword);
            this.Controls.Add(btnLogin);
            this.Controls.Add(btnGuest);
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            string login = txtLogin.Text;
            string password = txtPassword.Text;

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                try
                {
                    conn.Open();
                    string query = "SELECT RoleId, LastName, FirstName, MiddleName FROM [User] WHERE Login = @login AND Password = @password";

                    using (SqlCommand cmd = new SqlCommand(query, conn))
                    {
                        cmd.Parameters.AddWithValue("@login", login);
                        cmd.Parameters.AddWithValue("@password", password);

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                int roleId = Convert.ToInt32(reader["RoleId"]);
                                string fullName = $"{reader["LastName"]} {reader["FirstName"]} {reader["MiddleName"]}".Trim();

                                CurrentUser user = new CurrentUser(roleId, fullName);
                                
                                MainForm mainForm = new MainForm(user);
                                mainForm.Show();
                                this.Hide(); 
                            }
                            else
                            {
                                MessageBox.Show("Неверный логин или пароль!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ошибка БД: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void BtnGuest_Click(object sender, EventArgs e)
        {
            CurrentUser guest = new CurrentUser();
            MainForm mainForm = new MainForm(guest);
            mainForm.Show();
            this.Hide();
        }
    }
}