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"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:PrototypWPFHAG"
|
||||
mc:Ignorable="d"
|
||||
Title="Prototype"
|
||||
Height="450"
|
||||
Width="800"
|
||||
TitleCharacterCasing="Normal"
|
||||
WindowTitleBrush="Firebrick"
|
||||
Icon="pack://application:,,,/Images/databaseicon.png"
|
||||
ResizeMode="CanResizeWithGrip">
|
||||
<Grid>
|
||||
<Border BorderBrush="Firebrick" BorderThickness="3">
|
||||
</Border>
|
||||
<StackPanel>
|
||||
<TextBlock Width="120" Margin="100" Background="AliceBlue"></TextBlock>
|
||||
<Button Height="25" Width="44" Content="Log in"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</mah:MetroWindow>
|
||||
<mah:MetroWindow xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
|
||||
x:Class="PrototypWPFHAG.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:PrototypWPFHAG"
|
||||
mc:Ignorable="d"
|
||||
Title="Prototype"
|
||||
Height="450"
|
||||
Width="800"
|
||||
TitleCharacterCasing="Normal"
|
||||
WindowTitleBrush="Firebrick"
|
||||
Icon="pack://application:,,,/Images/databaseicon.png"
|
||||
ResizeMode="CanResizeWithGrip">
|
||||
<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" />
|
||||
<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" />
|
||||
|
||||
<!-- 登录按钮 -->
|
||||
<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()
|
||||
{
|
||||
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()
|
||||
|
@ -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