app.config gelöscht. MainWindow mit Datenbank verbunden
This commit is contained in:
parent
0105bf1025
commit
fdf61b30df
@ -1,23 +1,32 @@
|
|||||||
<mah:MetroWindow xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" x:Class="PrototypWPFHAG.MainWindow"
|
<mah:MetroWindow xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
x:Class="PrototypWPFHAG.MainWindow"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:local="clr-namespace:PrototypWPFHAG"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
mc:Ignorable="d"
|
xmlns:local="clr-namespace:PrototypWPFHAG"
|
||||||
Title="Prototype"
|
mc:Ignorable="d"
|
||||||
Height="450"
|
Title="Prototype"
|
||||||
Width="800"
|
Height="450"
|
||||||
TitleCharacterCasing="Normal"
|
Width="800"
|
||||||
WindowTitleBrush="Firebrick"
|
TitleCharacterCasing="Normal"
|
||||||
Icon="pack://application:,,,/Images/databaseicon.png"
|
WindowTitleBrush="Firebrick"
|
||||||
ResizeMode="CanResizeWithGrip">
|
Icon="pack://application:,,,/Images/databaseicon.png"
|
||||||
<Grid>
|
ResizeMode="CanResizeWithGrip">
|
||||||
<Border BorderBrush="Firebrick" BorderThickness="3">
|
<Grid>
|
||||||
</Border>
|
<Border BorderBrush="Firebrick" BorderThickness="3" Padding="20">
|
||||||
<StackPanel>
|
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
|
||||||
<TextBlock Width="120" Margin="100" Background="AliceBlue"></TextBlock>
|
<!-- 用户名标签和输入框 -->
|
||||||
<Button Height="25" Width="44" Content="Log in"/>
|
<TextBlock Text="USERNAME" Margin="0,10,0,0" FontSize="14" FontWeight="Bold" />
|
||||||
</StackPanel>
|
<TextBox Width="200" Height="30" Margin="0,5,0,0" x:Name="UsernameTextBox" />
|
||||||
</Grid>
|
|
||||||
</mah:MetroWindow>
|
<!-- 密码标签和输入框 -->
|
||||||
|
<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" />
|
||||||
|
|
||||||
|
<!-- 登录按钮 -->
|
||||||
|
<Button Height="30" Width="100" Content="Log in" Margin="0,20,0,0" Click="loginButton_Click" />
|
||||||
|
</StackPanel>
|
||||||
|
</Border>
|
||||||
|
</Grid>
|
||||||
|
</mah:MetroWindow>
|
@ -23,10 +23,49 @@ public partial class MainWindow : MetroWindow
|
|||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
// TestConnection();
|
||||||
|
// Console.ReadKey();
|
||||||
|
|
||||||
TestConnection();
|
}
|
||||||
Console.ReadKey();
|
|
||||||
|
|
||||||
|
private void loginButton_Click(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool ValidateUser(string username, string password)
|
||||||
|
{
|
||||||
|
// 获取数据库连接
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void TestConnection()
|
private static void TestConnection()
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
<connectionStrings>
|
|
||||||
<add name="MyConnectionString"
|
|
||||||
providerName="Npgsql"
|
|
||||||
connectionString="Server=127.0.0.1;Port=7854;
|
|
||||||
Database=mydatabase;User Id=admin;Password=123456;" />
|
|
||||||
</connectionStrings>
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user