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


================ ФАЙЛ: MainWindow.xaml ================
Назначение: внешний вид окна входа.

<Window x:Class="Authorization.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Авторизация"
        Height="300"
        Width="400"
        WindowStartupLocation="CenterScreen">

    <Grid Margin="20">
        <StackPanel>
            <TextBlock Text="Авторизация"
                       FontSize="24"
                       HorizontalAlignment="Center"
                       Margin="0,0,0,20"/>

            <TextBlock Text="Логин"/>

            <TextBox x:Name="TbLogin"
                     Height="28"
                     Margin="0,5,0,15"/>

            <TextBlock Text="Пароль"/>

            <PasswordBox x:Name="PbPass"
                         Height="28"
                         Margin="0,5,0,20"/>

            <Button Content="Войти"
                    Width="150"
                    Height="30"
                    Click="ButtonEnter_Click"/>
        </StackPanel>
    </Grid>
</Window>


================ ФАЙЛ: MainWindow.xaml.cs ================
Назначение: подключение к базе и проверка логина и пароля.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows;

namespace Authorization
{
    public partial class MainWindow : Window
    {
        string connectionString =
            @"Data Source=201-01\SQLEXPRESS;Initial Catalog=ToyShopDB;Integrated Security=True;Connect Timeout=5;";

        public MainWindow()
        {
            InitializeComponent();
        }

        public DataTable Select(string selectSQL)
        {
            DataTable table = new DataTable();

            using (SqlConnection connection =
                   new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(selectSQL, connection);

                command.Parameters.Add("@login", SqlDbType.NVarChar, 50)
                    .Value = TbLogin.Text;

                command.Parameters.Add("@password", SqlDbType.NVarChar, 100)
                    .Value = PbPass.Password;

                SqlDataAdapter adapter = new SqlDataAdapter(command);

                connection.Open();
                adapter.Fill(table);
            }

            return table;
        }

        private void ButtonEnter_Click(object sender, RoutedEventArgs e)
        {
            if (TbLogin.Text == "" || PbPass.Password == "")
            {
                MessageBox.Show("Введите логин и пароль");
                return;
            }

            try
            {
                DataTable table = Select(
                    "SELECT * FROM dbo.Users " +
                    "WHERE [Login] = @login AND [PasswordHash] = @password");

                if (table.Rows.Count > 0)
                {
                    WindowProduct window = new WindowProduct();
                    window.Show();
                    Close();
                }
                else
                {
                    MessageBox.Show("Неверный логин или пароль");
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
            }
        }
    }
}


================ ФАЙЛ: WindowProduct.xaml ================
Назначение: окно после успешного входа.

<Window x:Class="Authorization.WindowProduct"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Товары"
        Height="350"
        Width="500"
        WindowStartupLocation="CenterScreen">

    <Grid Margin="20">
        <StackPanel VerticalAlignment="Center">
            <TextBlock Text="Магазин игрушек"
                       FontSize="26"
                       HorizontalAlignment="Center"/>

            <TextBlock Text="Вход выполнен!"
                       FontSize="18"
                       HorizontalAlignment="Center"
                       Margin="0,20,0,0"/>
        </StackPanel>
    </Grid>
</Window>


================ ФАЙЛ: WindowProduct.xaml.cs ================
Назначение: создание второго окна.

using System.Windows;

namespace Authorization
{
    public partial class WindowProduct : Window
    {
        public WindowProduct()
        {
            InitializeComponent();
        }
    }
}