MOD: MainWindow with simple log in logic + connection to PostgreSQL (Database)
ADD: SearchWindow with logic from Suchenfenster + "Zurück"-Button to change back to MainWindow DEL: SuchenFenster (exhcanged with Search Window)
This commit is contained in:
		@@ -12,21 +12,36 @@
 | 
			
		||||
                 TitleCharacterCasing="Normal"
 | 
			
		||||
                 WindowTitleBrush="Firebrick"
 | 
			
		||||
                 Icon="pack://application:,,,/Images/databaseicon.png"
 | 
			
		||||
                 ResizeMode="CanResizeWithGrip">
 | 
			
		||||
                 ResizeMode="CanResizeWithGrip"
 | 
			
		||||
                 Loaded="Window_Loaded">
 | 
			
		||||
    <Grid>
 | 
			
		||||
        <Border BorderBrush="Firebrick" BorderThickness="3" Padding="20">
 | 
			
		||||
            <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
 | 
			
		||||
                <!-- 用户名标签和输入框 -->
 | 
			
		||||
                <TextBlock Text="USERNAME" Margin="0,10,0,0" FontSize="14" FontWeight="Bold" />
 | 
			
		||||
 | 
			
		||||
                <!-- Username -->
 | 
			
		||||
                <TextBlock Text="Benutzername" Margin="0,10,0,0" FontSize="14" FontWeight="Bold" />
 | 
			
		||||
                <TextBox Width="200" Height="30" Margin="0,5,0,0" x:Name="UsernameTextBox" />
 | 
			
		||||
 | 
			
		||||
                <!-- 密码标签和输入框 -->
 | 
			
		||||
                <TextBlock Text="PASSWORD" Margin="0,10,0,0" FontSize="14" FontWeight="Bold" />
 | 
			
		||||
                <PasswordBox Width="200" Height="30" Margin="0,5,0,0" x:Name="PasswordTextBox" />
 | 
			
		||||
                <TextBlock Text="Passwort" Margin="0,10,0,0" FontSize="14" FontWeight="Bold" />
 | 
			
		||||
                <Grid>
 | 
			
		||||
                    <!-- Passwortbox (hidden when Password is visible) -->
 | 
			
		||||
                    <PasswordBox Width="170" Height="30" Margin="0,5,34,0" x:Name="PasswordTextBox"
 | 
			
		||||
                    PasswordChanged="PasswordTextBox_PasswordChanged"/>
 | 
			
		||||
 | 
			
		||||
                <!-- 登录按钮 -->
 | 
			
		||||
                <Button Height="30" Width="100" Content="Log in" Margin="0,20,0,0" Click="loginButton_Click" />
 | 
			
		||||
                    <!-- Textbox for visible password (hidden at default) -->
 | 
			
		||||
                    <TextBox Width="170" Height="30" Margin="0,5,34,0" x:Name="PasswordVisibleTextBox"
 | 
			
		||||
                    Visibility="Collapsed" IsReadOnly="True"/>
 | 
			
		||||
 | 
			
		||||
                    <!-- Button with Eye-Icon -->
 | 
			
		||||
                    <Button Width="30" Height="30" Margin="175,5,0,0" Content="👁" 
 | 
			
		||||
                    Click="ShowPasswordButton_Click"/>
 | 
			
		||||
                </Grid>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                <!-- Login Button -->
 | 
			
		||||
                <Button Height="30" Width="100" Content="Zur Suche" Margin="0,20,0,0" 
 | 
			
		||||
                        Click="loginButton_Click" IsDefault="True" />
 | 
			
		||||
            </StackPanel>
 | 
			
		||||
        </Border>
 | 
			
		||||
    </Grid>
 | 
			
		||||
</mah:MetroWindow>
 | 
			
		||||
</mah:MetroWindow>
 | 
			
		||||
 
 | 
			
		||||
@@ -23,86 +23,100 @@ public partial class MainWindow : MetroWindow
 | 
			
		||||
    public MainWindow()
 | 
			
		||||
    {
 | 
			
		||||
        InitializeComponent();
 | 
			
		||||
       // TestConnection();
 | 
			
		||||
       // Console.ReadKey();
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void loginButton_Click(object sender, RoutedEventArgs e)
 | 
			
		||||
    private void Window_Loaded(object sender, RoutedEventArgs e)
 | 
			
		||||
    {
 | 
			
		||||
        // 从用户输入中获取用户名和密码
 | 
			
		||||
        string username = UsernameTextBox.Text;          // 获取用户名
 | 
			
		||||
        string password = PasswordTextBox.Password;      // 获取密码
 | 
			
		||||
 | 
			
		||||
        // 调用 ValidateUser 方法验证用户凭据
 | 
			
		||||
        if (ValidateUser(username, password))
 | 
			
		||||
        {
 | 
			
		||||
            MessageBox.Show("ok", "Login Successful", MessageBoxButton.OK, MessageBoxImage.Error);
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            // 如果验证失败,显示错误信息
 | 
			
		||||
            MessageBox.Show("Invalid username or password.", "Login Failed", MessageBoxButton.OK, MessageBoxImage.Error);
 | 
			
		||||
        }
 | 
			
		||||
        UsernameTextBox.Focus(); // Focus first TextBox on start
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private bool ValidateUser(string username, string password)
 | 
			
		||||
    private static NpgsqlConnection GetConnection()
 | 
			
		||||
    {
 | 
			
		||||
        // 获取数据库连接
 | 
			
		||||
        using (var con = GetConnection())
 | 
			
		||||
        {
 | 
			
		||||
            con.Open(); // 打开数据库连接
 | 
			
		||||
            string query = "SELECT COUNT(*) FROM USER WHERE UserName = @username AND Password = @password;";
 | 
			
		||||
 | 
			
		||||
            using (var cmd = new NpgsqlCommand(query, con))
 | 
			
		||||
            {
 | 
			
		||||
                // 使用参数化查询防止 SQL 注入
 | 
			
		||||
                cmd.Parameters.AddWithValue("@username", username);
 | 
			
		||||
                cmd.Parameters.AddWithValue("@password", password);
 | 
			
		||||
 | 
			
		||||
                // 执行查询并获取结果
 | 
			
		||||
                int count = (int)cmd.ExecuteScalar();
 | 
			
		||||
                return count > 0; // 如果匹配记录数量大于 0,返回 true
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=postgres;Database=postgres;");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static void TestConnection()
 | 
			
		||||
    private string ValidateUser(string username, string password)
 | 
			
		||||
    {
 | 
			
		||||
        using (NpgsqlConnection con = GetConnection())
 | 
			
		||||
        {
 | 
			
		||||
            con.Open();
 | 
			
		||||
            if (con.State == ConnectionState.Open)
 | 
			
		||||
 | 
			
		||||
            // Check for correct UserName
 | 
			
		||||
            string userQuery = "SELECT COUNT(*) FROM \"User\" WHERE \"UserName\" = @username";
 | 
			
		||||
            using (NpgsqlCommand userCmd = new NpgsqlCommand(userQuery, con))
 | 
			
		||||
            {
 | 
			
		||||
                Console.WriteLine("Connected to Server");
 | 
			
		||||
                userCmd.Parameters.AddWithValue("@username", username);
 | 
			
		||||
                int userExists = Convert.ToInt32(userCmd.ExecuteScalar());
 | 
			
		||||
 | 
			
		||||
                if (userExists == 0)
 | 
			
		||||
                {
 | 
			
		||||
                    return "Dieser Benutzername existiert nicht";
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            // Check for correct Password
 | 
			
		||||
            string pwQuery = "SELECT COUNT(*) FROM \"User\" WHERE \"UserName\" = @username AND \"Password\" = @password";
 | 
			
		||||
            using (NpgsqlCommand pwCmd = new NpgsqlCommand(pwQuery, con))
 | 
			
		||||
            {
 | 
			
		||||
                pwCmd.Parameters.AddWithValue("@username", username);
 | 
			
		||||
                pwCmd.Parameters.AddWithValue("@password", password);
 | 
			
		||||
                int correctPassword = Convert.ToInt32(pwCmd.ExecuteScalar());
 | 
			
		||||
 | 
			
		||||
                if (correctPassword == 0)
 | 
			
		||||
                {
 | 
			
		||||
                    return "Falsches Password!";
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    private static NpgsqlConnection GetConnection()
 | 
			
		||||
    {
 | 
			
		||||
        return new NpgsqlConnection(@"Server=localhost;Port=5432;User Id=postgres;Password=postgres;Database=postgres;");
 | 
			
		||||
 | 
			
		||||
        return "Erfolgreich";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //class Program
 | 
			
		||||
    //{
 | 
			
		||||
    //    static void Main(string[] args)
 | 
			
		||||
    //    {
 | 
			
		||||
    //        TestConnection();
 | 
			
		||||
    //        Console.ReadKey();
 | 
			
		||||
    //    }
 | 
			
		||||
    //    private static void TestConnection()
 | 
			
		||||
    //    {
 | 
			
		||||
    //        using(NpgsqlConnection con = GetConnection()){
 | 
			
		||||
    //            con.Open();
 | 
			
		||||
    //            if (con.State == ConnectionState.Open) {
 | 
			
		||||
    //                Console.WriteLine("Connected to Server");
 | 
			
		||||
    //            }
 | 
			
		||||
    //        }
 | 
			
		||||
    //    }
 | 
			
		||||
    //    private static NpgsqlConnection GetConnection()
 | 
			
		||||
    //    {
 | 
			
		||||
    //        return new NpgsqlConnection(@"Server=localhost;Port=5432;User Id=postgres;Password=postgres;Database=TestServer;");
 | 
			
		||||
    //    }
 | 
			
		||||
    //}
 | 
			
		||||
    private void ShowPasswordButton_PreviewMouseDown(object sender, RoutedEventArgs e)
 | 
			
		||||
    {
 | 
			
		||||
        PasswordVisibleTextBox.Text = PasswordTextBox.Password; // Write password in the visible box
 | 
			
		||||
        PasswordVisibleTextBox.Visibility = Visibility.Visible;
 | 
			
		||||
        PasswordTextBox.Visibility = Visibility.Collapsed;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void ShowPasswordButton_Click(object sender, RoutedEventArgs e)
 | 
			
		||||
    {
 | 
			
		||||
        if (PasswordTextBox.Visibility == Visibility.Visible)
 | 
			
		||||
        {
 | 
			
		||||
            // Show password
 | 
			
		||||
            PasswordVisibleTextBox.Text = PasswordTextBox.Password;
 | 
			
		||||
            PasswordVisibleTextBox.Visibility = Visibility.Visible;
 | 
			
		||||
            PasswordTextBox.Visibility = Visibility.Collapsed;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            // Hide password
 | 
			
		||||
            PasswordTextBox.Visibility = Visibility.Visible;
 | 
			
		||||
            PasswordVisibleTextBox.Visibility = Visibility.Collapsed;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Syncs password with visible TextBox
 | 
			
		||||
    private void PasswordTextBox_PasswordChanged(object sender, RoutedEventArgs e)
 | 
			
		||||
    {
 | 
			
		||||
        PasswordVisibleTextBox.Text = PasswordTextBox.Password;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void loginButton_Click(object sender, RoutedEventArgs e)
 | 
			
		||||
    {
 | 
			
		||||
        string username = UsernameTextBox.Text;
 | 
			
		||||
        string password = PasswordTextBox.Password;
 | 
			
		||||
        string result = ValidateUser(username, password);
 | 
			
		||||
 | 
			
		||||
        if (result == "Erfolgreich")
 | 
			
		||||
        {
 | 
			
		||||
            SearchWindow searchWindow = new();
 | 
			
		||||
            searchWindow.Show(); // Open new window
 | 
			
		||||
            this.Close(); // Close this window
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            MessageBox.Show(result, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -4,7 +4,7 @@
 | 
			
		||||
 | 
			
		||||
    <OutputType>WinExe</OutputType>
 | 
			
		||||
 | 
			
		||||
    <OutputType>Exe</OutputType>
 | 
			
		||||
    <OutputType>WinExe</OutputType>
 | 
			
		||||
 | 
			
		||||
    <TargetFramework>net9.0-windows</TargetFramework>
 | 
			
		||||
    <Nullable>enable</Nullable>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,8 @@
 | 
			
		||||
<Window x:Class="SuchenFenster.MainWindow"
 | 
			
		||||
<Window x:Class="PrototypWPFHAG.SearchWindow"
 | 
			
		||||
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 | 
			
		||||
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 | 
			
		||||
        Title="Suchen Fenster" Height="600" Width="800">
 | 
			
		||||
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
 | 
			
		||||
        xmlns:local="clr-namespace:PrototypWPFHAG"
 | 
			
		||||
        Title="Such Fenster" Height="600" Width="800">
 | 
			
		||||
    <Grid>
 | 
			
		||||
        <!-- 左侧垂直布局 -->
 | 
			
		||||
        <Grid.ColumnDefinitions>
 | 
			
		||||
@@ -40,6 +41,7 @@
 | 
			
		||||
                </Border>
 | 
			
		||||
                <Button Content="ADD" Width="60" Height="30" Grid.Column="1" Margin="5,0,0,0"/>
 | 
			
		||||
            </Grid>
 | 
			
		||||
            <Button Content="Zurück " Margin="5,380" Click="BackToLogIn_Click"/>
 | 
			
		||||
        </StackPanel>
 | 
			
		||||
 | 
			
		||||
        <!-- 右侧区域 -->
 | 
			
		||||
@@ -47,19 +49,19 @@
 | 
			
		||||
            <Label Content="Zeugnisse:" HorizontalAlignment="Left" Margin="0,2,0,8"/>
 | 
			
		||||
            <!-- ListField -->
 | 
			
		||||
            <ListBox x:Name="ListField" Margin="0,29,0,35" BorderThickness="1" BorderBrush="Black">
 | 
			
		||||
    <ListBox.ItemTemplate>
 | 
			
		||||
        <DataTemplate>
 | 
			
		||||
            <StackPanel Orientation="Vertical">
 | 
			
		||||
                <!-- 内容 -->
 | 
			
		||||
                <TextBlock Text="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Padding="5"/>
 | 
			
		||||
                <!-- 分隔线 -->
 | 
			
		||||
                <Border Height="1" Background="Black" Margin="0,2,0,2"/>
 | 
			
		||||
            </StackPanel>
 | 
			
		||||
        </DataTemplate>
 | 
			
		||||
    </ListBox.ItemTemplate>
 | 
			
		||||
</ListBox>
 | 
			
		||||
                <ListBox.ItemTemplate>
 | 
			
		||||
                    <DataTemplate>
 | 
			
		||||
                        <StackPanel Orientation="Vertical">
 | 
			
		||||
                            <!-- 内容 -->
 | 
			
		||||
                            <TextBlock Text="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Padding="5"/>
 | 
			
		||||
                            <!-- 分隔线 -->
 | 
			
		||||
                            <Border Height="1" Background="Black" Margin="0,2,0,2"/>
 | 
			
		||||
                        </StackPanel>
 | 
			
		||||
                    </DataTemplate>
 | 
			
		||||
                </ListBox.ItemTemplate>
 | 
			
		||||
            </ListBox>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
           
 | 
			
		||||
            <!-- 删除按钮 -->
 | 
			
		||||
            <Button Content="Löschen" Width="100" Height="30" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
 | 
			
		||||
        </Grid>
 | 
			
		||||
							
								
								
									
										34
									
								
								PrototypWPFHAG/SearchWindow.xaml.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								PrototypWPFHAG/SearchWindow.xaml.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using System.Windows;
 | 
			
		||||
using System.Windows.Controls;
 | 
			
		||||
using System.Windows.Data;
 | 
			
		||||
using System.Windows.Documents;
 | 
			
		||||
using System.Windows.Input;
 | 
			
		||||
using System.Windows.Media;
 | 
			
		||||
using System.Windows.Media.Imaging;
 | 
			
		||||
using System.Windows.Shapes;
 | 
			
		||||
 | 
			
		||||
namespace PrototypWPFHAG
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Interaktionslogik für SearchWindow.xaml
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public partial class SearchWindow : Window
 | 
			
		||||
    {
 | 
			
		||||
        public SearchWindow()
 | 
			
		||||
        {
 | 
			
		||||
            InitializeComponent();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void BackToLogIn_Click(object sender, RoutedEventArgs e)
 | 
			
		||||
        {
 | 
			
		||||
                MainWindow loginWindow = new();
 | 
			
		||||
                loginWindow.Show(); // Open new window
 | 
			
		||||
                this.Close(); // Close this window
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1 +0,0 @@
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user