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


using System;
using System.Data;
using System.Windows;
using MySqlConnector;

namespace LabApp
{
    public partial class LoginWindow : Window
    {
        private string cs = "Server=localhost;Port=3306;Database=lab_accounting_db;Uid=root;Pwd=12345;";

        public LoginWindow()
        {
            InitializeComponent();
        }

        private DataTable Q(string sql, params MySqlParameter[] p)
        {
            DataTable t = new DataTable();

            using (MySqlConnection c = new MySqlConnection(cs))
            {
                using (MySqlCommand m = new MySqlCommand(sql, c))
                {
                    if (p != null && p.Length > 0)
                    {
                        m.Parameters.AddRange(p);
                    }

                    using (MySqlDataAdapter a = new MySqlDataAdapter(m))
                    {
                        a.Fill(t);
                    }
                }
            }

            return t;
        }

        private void LoginClick(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbLogin.Text) || string.IsNullOrWhiteSpace(pbPassword.Password))
            {
                tbMsg.Text = "Введите логин и пароль";
                return;
            }

            try
            {
                DataTable t = Q(
                    @"select u.UserID, u.FullName, r.RoleName
                      from Users u
                      join Roles r on r.RoleID = u.RoleID
                      where u.Login = @l and u.PasswordHash = @p",
                    new MySqlParameter("@l", tbLogin.Text.Trim()),
                    new MySqlParameter("@p", pbPassword.Password.Trim())
                );

                if (t.Rows.Count == 0)
                {
                    tbMsg.Text = "Неверный логин или пароль";
                    return;
                }

                DataRow row = t.Rows[0];
                int userId = Convert.ToInt32(row["UserID"]);
                string fullName = Convert.ToString(row["FullName"]);
                string roleName = Convert.ToString(row["RoleName"]);

                MainWindow w = new MainWindow(userId, fullName, roleName);
                w.Show();
                Close();
            }
            catch (Exception ex)
            {
                tbMsg.Text = ex.Message;
            }
        }
    }
}