WPF 登陆界面判断输入的用户名和密码是否与数据库中保存的数据相同

数据库的相关信息:服务器名称(w-PC\Example),数据库名(WW),数据表名称(WW_user)。
数据库的字段是:用户名(UserName),密码(Password)。
登陆界面的各个属性为:用户名输入框(txt_UserName),密码输入框(txt_Password,我用的是PasswordBox),登陆按钮(but_load)。

private void but_load_Click(object sender, RoutedEventArgs e)
    {
        SqlConnectionStringBuilder connbuilder = new SqlConnectionStringBuilder();
        connbuilder.DataSource = "w-PC\\Example";//服务器名称
        connbuilder.IntegratedSecurity = true;//登录方式,true为 Windows 身份验证
        connbuilder.InitialCatalog = "WW";//数据库名

        SqlConnection conn = new SqlConnection(connbuilder.ConnectionString);
        conn.Open();
        string a = "select * from WW_user where UserName='" + txt_UserName.Text + "' and Password='" + txt_Password.Password + "'";
        SqlCommand comm = new SqlCommand(a, conn);
        SqlDataReader dr = comm.ExecuteReader();
        if (txt_UserName.Text == "" || txt_Password.Password == "") //判断输入是否为空
        {
            MessageBox.Show("请填写用户名和密码"); 
        }

        else
        {
            if (dr.Read())//判断是否存在用户输入的用户名和密码
            {
                MessageBox.Show("登陆成功");
                conn.Close();
            }

            else
            {
                MessageBox.Show("用户名或密码有误");
                conn.Close();
            }
        }

    }
  • 7
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,下面是一个简单的 WPF 登录界面的示例代码,包括用户名密码输入框和登录按钮,您可以根据自己的需求进行修改和完善。 ```xaml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="登录" Height="200" Width="300"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Label Content="用户名:" Grid.Row="0" Grid.Column="0" Margin="10"/> <TextBox x:Name="txtUserName" Grid.Row="0" Grid.Column="1" Margin="10"/> <Label Content="密码:" Grid.Row="1" Grid.Column="0" Margin="10"/> <PasswordBox x:Name="pwdPassword" Grid.Row="1" Grid.Column="1" Margin="10"/> <Button Content="登录" Grid.Row="2" Grid.Column="1" Margin="10" Click="btnLogin_Click"/> </Grid> </Window> ``` ```csharp using System.Windows; using System.Windows.Controls; namespace WpfApp1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btnLogin_Click(object sender, RoutedEventArgs e) { string userName = txtUserName.Text; // 获取输入用户名 string password = pwdPassword.Password; // 获取输入密码 // 在这里处理登录逻辑,例如验证用户名密码是否正确 if (userName == "admin" && password == "123456") { MessageBox.Show("登录成功!"); } else { MessageBox.Show("用户名密码错误!"); } } } } ``` 在这个示例,我们使用了 `TextBox` 和 `PasswordBox` 控件来实现用户名密码输入,使用 `Button` 控件来实现登录按钮,并在代码实现了登录逻辑。您可以根据自己的需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值