`
yanfaguanli
  • 浏览: 660571 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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

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

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

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

1:新建一个窗口LoginWindow.xaml

<Grid>

<GridName="m_grid"><!--三行两列-->

<Grid.ColumnDefinitions>

<ColumnDefinition></ColumnDefinition>

<ColumnDefinition></ColumnDefinition>

</Grid.ColumnDefinitions>

<Grid.RowDefinitions>

<RowDefinition></RowDefinition>

<RowDefinition></RowDefinition>

<RowDefinition></RowDefinition>

</Grid.RowDefinitions>

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

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

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

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

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

</Grid>

</Grid>

2:数据库新建一张表T_Users

staticinterrortime=3;//记录剩余次数

privatevoidm_bt_login_Click(objectsender,RoutedEventArgse)

{

//用户名是否存在¨

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

stringusername=m_txt_user.Text;

stringpassword=m_pbox_password.Password;

using(SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=test;UserId=sa;Password=123456"))

{

conn.Open();

using(SqlCommandcmd=conn.CreateCommand())

{

cmd.CommandText="selectUserNamefromT_Users";

using(SqlDataReaderreader=cmd.ExecuteReader())

{

while(reader.Read())

{

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

{

count+=1;

}

}

}

}

}

if(count<=0)

{

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

return;

}

else

{

using(SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=test;UserId=sa;Password=123456"))

{

conn.Open();

using(SqlCommandcmd=conn.CreateCommand())

{

cmd.CommandText="selectUserName,Password,ErrorTimefromT_Users";

using(SqlDataReaderreader=cmd.ExecuteReader())

{

while(reader.Read())

{

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

{

if(--errortime>0)

{

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

return;

}

else

{

cmd.CommandText="updateT_UserssetErrorTime=GETDATE()whereUserName='admin'";//这里不知为何无法更新数据库数据(~语句写错了。。)

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

return;

}

}

}

}

}

}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics