gpt4 book ai didi

c# - 使用 C#、XAML 和 MySQL 进行基本身份验证

转载 作者:行者123 更新时间:2023-11-30 00:01:39 25 4
gpt4 key购买 nike

我花了几天时间在 C# 项目中实现基本登录表单,但一点运气都没有。

登录表单由两个文本框组成:第一个用于用户名,第二个用于密码。

当用户单击该按钮时,应根据 MySQL 服务器中的表检查凭据。如果凭据有效,用户应该导航到另一个窗口。

到目前为止,主窗口看起来像这样:

XAML 文件

<Controls:MetroWindow x:Class="MetroDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:MetroDemo="clr-namespace:MetroDemo"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:exampleViews="clr-namespace:MetroDemo.ExampleViews"
Title="LOGIN"
Width="500"
Height="700"
Icon="mahapps.metro.logo2.ico"
ShowIconOnTitleBar="False"
ShowTitleBar="False"
WindowStartupLocation="CenterScreen"
GlowBrush="{DynamicResource AccentColorBrush}"
mc:Ignorable="d"
d:DesignHeight="600"
d:DesignWidth="800"
d:DataContext="{d:DesignInstance MetroDemo:MainWindowViewModel}"
Closing="MetroWindow_Closing"
Style="{DynamicResource VSWindowStyleKey}">

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/VS/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/VS/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>

<Canvas x:Key="appbar_github"
Width="48"
Height="48"
Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0"
UseLayoutRounding="False">
<Path Width="43.4333"
Height="34.724"
Canvas.Left="2.06666"
Canvas.Top="9.0482"
Stretch="Fill"
Fill="{DynamicResource BlackBrush}"
Data="F1 M 20.4727,24.1087C 20.4727,25.2202 19.8462,26.1209 19.0743,26.1209C 18.3018,26.1209 17.6761,25.2202 17.6761,24.1087C 17.6761,22.9967 18.3018,22.0955 19.0743,22.0955C 19.8462,22.0955 20.4727,22.9967 20.4727,24.1087 Z M 31.7243,24.1087C 31.7243,25.2202 31.0982,26.1209 30.3259,26.1209C 29.5539,26.1209 28.9278,25.2202 28.9278,24.1087C 28.9278,22.9967 29.5539,22.0955 30.3259,22.0955C 31.0982,22.0955 31.7243,22.9967 31.7243,24.1087 Z M 25.0517,26.752C 25.0517,27.0417 24.8168,27.2766 24.5271,27.2766C 24.2372,27.2766 24.0026,27.0417 24.0026,26.752C 24.0026,26.4623 24.2372,26.2275 24.5271,26.2275C 24.8168,26.2275 25.0517,26.4623 25.0517,26.752 Z M 23.3051,28.0781C 23.2741,27.9901 23.3204,27.894 23.4079,27.8631C 23.4957,27.8319 23.5922,27.8782 23.6232,27.9657C 23.758,28.3467 24.1193,28.6027 24.5227,28.6027C 24.926,28.6027 25.2874,28.3467 25.4224,27.9657C 25.4533,27.8777 25.5497,27.8319 25.6377,27.8631C 25.7254,27.894 25.7712,27.9901 25.7403,28.0781C 25.5579,28.5933 25.0683,28.9396 24.5227,28.9396C 23.977,28.9396 23.4879,28.5933 23.3051,28.0781 Z M 45.4666,25.8891C 41.7383,25.1435 39.5769,25.1425 37.2874,25.2403C 37.2044,25.5172 37.1111,25.7865 37.004,26.0452C 39.2636,26.1111 41.5963,27.0572 45.4253,28.1372L 45.3796,28.2994C 41.5286,27.2128 39.1693,26.2736 36.9323,26.2122C 35.8027,28.7747 33.3519,30.4238 28.2447,30.9302C 29.1208,31.5249 30.0168,32.6517 30.0168,34.7794L 30.0168,40.2354C 30.0168,40.2354 30.1292,41.5855 31.3669,42.0354C 31.3669,42.0354 32.0976,42.5419 31.3103,42.8227C 31.3103,42.8227 27.8793,43.1041 27.8793,40.2917L 27.8793,36.1295C 27.8793,36.1295 28.0148,34.5951 27.2044,33.9639L 27.2044,40.7416C 27.2044,40.7416 27.2604,42.3729 28.1041,42.9917C 28.1041,42.9917 28.6664,44.0039 27.4294,43.723C 27.4294,43.723 25.0669,43.3855 24.9545,40.6292L 24.9004,33.7107L 24.3329,33.7107L 24.2791,40.6292C 24.1664,43.3855 21.8042,43.723 21.8042,43.723C 20.5667,44.0039 21.1292,42.9917 21.1292,42.9917C 21.9731,42.3729 22.029,40.7416 22.029,40.7416L 22.029,33.905C 21.2185,34.4786 21.3543,36.1295 21.3543,36.1295L 21.3543,40.2917C 21.3543,43.1041 17.923,42.8227 17.923,42.8227C 17.1355,42.5419 17.8667,42.0354 17.8667,42.0354C 19.1044,41.5855 19.2168,40.2354 19.2168,40.2354L 19.2168,36.4182C 13.9026,37.6038 12.8015,33.9086 12.8015,33.9086C 11.9575,31.7419 10.7337,31.1653 10.7337,31.1653C 9.03174,29.998 10.8601,30.0259 10.8601,30.0259C 12.7451,30.167 13.7299,31.9675 13.7299,31.9675C 15.4891,34.9934 18.4212,33.9118 19.3473,33.469C 19.6287,32.1551 20.3218,31.3786 21.0017,30.9219C 16.2126,30.4057 13.5206,28.7618 12.1552,26.2167C 9.892,26.2859 5.95582,27.5698 2.18687,28.6327L 2.1411,28.4706C 5.88063,27.4157 9.78237,26.1245 12.0677,26.0508C 11.9342,25.7865 11.815,25.5129 11.7081,25.231C 9.39687,25.1435 5.7021,24.8353 2.09986,25.5558L 2.06666,25.3903C 5.64753,24.6741 9.31942,24.975 11.6447,25.0595C 11.1589,23.6976 10.9597,22.1307 10.9597,20.3838C 10.9597,18.07 11.7961,16.1828 13.1661,14.7044C 12.9471,14.1686 12.2264,12.0107 13.3921,9.09626C 13.3921,9.09626 15.1775,8.53096 19.2226,11.2908C 20.9195,10.8253 22.7388,10.596 24.5393,10.596C 26.1759,10.596 27.8099,10.7853 29.3453,11.1691C 33.2647,8.54605 34.9973,9.09626 34.9973,9.09626C 35.9797,11.5523 35.6222,13.4712 35.3543,14.335C 36.886,15.8637 37.8295,17.8726 37.8295,20.3838C 37.8295,22.1347 37.7156,23.7049 37.3369,25.0693C 39.6423,24.9735 41.7864,24.981 45.5,25.7236L 45.4666,25.8891 Z M 32.8338,20.2655C 31.019,18.6227 27.9632,19.5073 24.4975,19.5073C 21.018,19.5073 17.9506,18.6315 16.1386,20.2856C 15.052,21.2782 14.4173,22.4741 14.4173,23.7621C 14.4173,29.142 18.9301,29.9804 24.4975,29.9804C 30.0642,29.9804 34.5775,29.8161 34.5775,23.7621C 34.5775,22.4656 33.934,21.2621 32.8338,20.2655 Z " />
</Canvas>

<Ellipse x:Key="AppThemeMenuIcon"
x:Shared="False"
Width="16"
Height="16"
StrokeThickness="1"
Stroke="{Binding BorderColorBrush, Mode=OneWay}"
Fill="{Binding ColorBrush, Mode=OneWay}" />
<Ellipse x:Key="AccentMenuIcon"
x:Shared="False"
Width="16"
Height="16"
Fill="{Binding ColorBrush, Mode=OneWay}" />

<Style x:Key="AppThemeMenuItemStyle"
TargetType="{x:Type MenuItem}"
BasedOn="{StaticResource MetroMenuItem}">
<Setter Property="Command"
Value="{Binding ChangeAccentCommand}" />
<Setter Property="Icon"
Value="{StaticResource AppThemeMenuIcon}" />
<Setter Property="Header"
Value="{Binding Name, Mode=OneWay}" />
</Style>

<Style x:Key="AccentColorMenuItemStyle"
TargetType="{x:Type MenuItem}"
BasedOn="{StaticResource MetroMenuItem}">
<Setter Property="Command"
Value="{Binding ChangeAccentCommand}" />
<Setter Property="Icon"
Value="{StaticResource AccentMenuIcon}" />
<Setter Property="Header"
Value="{Binding Name, Mode=OneWay}" />
</Style>



</ResourceDictionary>
</Window.Resources>




<Controls:MetroContentControl>

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>

</Grid.RowDefinitions>

<TextBox x:Name="textBoxUsername" HorizontalAlignment="Center" Height="30" Grid.Row="1" TextWrapping="Wrap" Text="Username" FontSize="14" Width="250"/>
<TextBox x:Name="textBoxPassword" HorizontalAlignment="Center" Height="30" Grid.Row="2" TextWrapping="Wrap" Text="Password" FontSize="14" VerticalAlignment="Top" Width="250"/>
<Button Grid.Row="3" Width="200" Height="50" Click="Connect" >LOGIN</Button>
</Grid>
</Controls:MetroContentControl>
</Controls:MetroWindow>

xaml.cs

using System;
using System.Threading.Tasks;
using System.Windows;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using MetroDemo.ExampleWindows;
using System.Data.SqlClient;

namespace MetroDemo
{
public partial class MainWindow
{

private bool _shutdown;
private readonly MainWindowViewModel _viewModel;

public MainWindow()
{
_viewModel = new MainWindowViewModel();
DataContext = _viewModel;
InitializeComponent();




}
private void Connect(object sender, RoutedEventArgs e) {

string pass, user;
string password = "";
string username = "";

user = textBoxUsername.Text;
pass = textBoxPassword.Text;
SqlConnection myConnection = new SqlConnection("user id=user;" +
"password=password;server=localhost;" +
"Trusted_Connection=yes;" +
"database=db; " +
"connection timeout=30");



SqlConnection Connect = new SqlConnection("SELECT * FROM users where username = @user AND password = @pass ");



if (found==0) // <-- THIS IS THE POINT WHERE I'M STUCK :P
{
new Dashboard().Show(); // USER AND PASSWORD ARE CORRECT

}

else
{
return; // USER AND PASSWORD ARE WRONG
}

}

有人能告诉我如何进行身份验证吗?

最佳答案

你还没有编写任何代码行来执行你的sql,你的逻辑将如何工作,这里我重写了代码

 T ExecuteScalar<T>(string commandText, Dictionary<string,object> parameters=null )
{
const string connectionString = @"user id=user;password=password;server=localhost;Trusted_Connection=yes;database=db;
connection timeout=30";

using (var connection = new SqlConnection(connectionString))
{
using (var sqlCommand = new SqlCommand())
{
sqlCommand.Connection = connection;
sqlCommand.CommandText = commandText;
if (parameters!=null)
{
foreach (var parameter in parameters)
{
sqlCommand.Parameters.Add(new SqlParameter(parameter.Key, parameter.Value));
}
}

return (T) sqlCommand.ExecuteScalar();
}
}
}

如何使用

var found= ExecuteScalar("SELECT * FROM users where username = @user AND password = @pass ", new Dictionary() { {"@user",userName}, {"@pass",password} });

        if (found > 0) /
new Dashboard().Show();
else
return; // USER AND PASSWORD ARE WRONG

关于c# - 使用 C#、XAML 和 MySQL 进行基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24983344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com