C# WPF 快速开发10登陆界面

功能:将用户名、密码存在数据库,连续三次密码输错需等待30分钟才能再次登录

看似小小的功能,也是调试花了很长的时间。本来是想着,数据库可以记录用户连续输错3次的时间点,如果判断用户是在30分钟后再次尝试登陆就清空时间点再次判断。但写了挺久才写了一半,因为还有很多东西要看,就先跳过了。

有个不好的地方,代码太多嵌套if语句了,到了后面维护应该比较困难。

下面代码也没什么用处的,作为数据库基本操作的练习而已。

1:新建一个窗口 LoginWindow.xaml

   <Grid>

        <Grid Name="m_grid"><!--三行两列-->

            <Grid.ColumnDefinitions>

                <ColumnDefinition></ColumnDefinition>

                <ColumnDefinition></ColumnDefinition>

            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>

                <RowDefinition></RowDefinition>

                <RowDefinition></RowDefinition>

                <RowDefinition></RowDefinition>

            </Grid.RowDefinitions>

            <TextBlock Text="用户名:" Grid.Row="0" Grid.Column="0"></TextBlock>

            <TextBlock Text="密码:" Grid.Row="1" Grid.Column="0"></TextBlock>

            <TextBox Name="m_txt_user" Grid.Row="0" Grid.Column="1"></TextBox>

            <PasswordBox Name="m_pbox_password" Grid.Row="1" Grid.Column="1"></PasswordBox>

            <Button Name="m_bt_login" Content="登陆" Grid.Row="2" Grid.Column="0"></Button>

        </Grid>

    </Grid>

2:数据库新建一张表T_Users

static int errortime = 3;//记录剩余次数

private void m_bt_login_Click(object sender, RoutedEventArgs e)

{

//用户名是否存在¨

int count = 0;//记录用户名是否存在¨

string username = m_txt_user.Text;

string password = m_pbox_password.Password;

using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;User Id=sa;Password=123456"))

{

    conn.Open();

    using (SqlCommand cmd = conn.CreateCommand())

    {

        cmd.CommandText = "select UserName from T_Users";

        using (SqlDataReader reader = cmd.ExecuteReader())

        {

            while (reader.Read())

            {

                if (username == reader.GetString(0))

                {

                    count += 1;

                }

            }

        }

    }

}

if (count <= 0)

{

    MessageBox.Show("没有此用户");

    return;

}

else

{

    using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;User Id=sa;Password=123456"))

    {

        conn.Open();

        using (SqlCommand cmd = conn.CreateCommand())

        {

            cmd.CommandText = "select UserName,Password,ErrorTime from T_Users";

            using (SqlDataReader reader = cmd.ExecuteReader())

            {

                while (reader.Read())

                {

                    if (password != reader.GetString(1))

                    {

                        if (--errortime > 0)

                        {

                            MessageBox.Show("你还有" + errortime + "次机会¨");

                            return;

                        }

                        else

                        {

                            cmd.CommandText = "update T_Users set ErrorTime=GETDATE()where UserName='admin'"//这里不知为何无法更新数据库数据(~语句写错了。。)

                            MessageBox.Show("请在30分钟后再尝试登陆");

                            return;

                        }

                    }

                }

            }

        }

    }

}

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值