登陆窗口练习

Code:
  1. namespace 登陆练习1   
  2. {   
  3.     public partial class Form1 : Form   
  4.     {   
  5.         public Form1()   
  6.         {   
  7.             InitializeComponent();   
  8.         }   
  9.   
  10.         private void incErrorTimes()   
  11.         {   
  12.             using (SqlConnection conn = new SqlConnection(@"Data Source=./SQLEXPRESS;   
  13. AttachDBFilename=D:/My Documents/Documents/Visual Studio 2010/Projects/登陆练习1/登陆练习1/MyDB.mdf;   
  14. Integrated Security=True;User Instance=True"))   
  15.             {   
  16.                 conn.Open();   
  17.                 using (SqlCommand cmd = conn.CreateCommand())   
  18.                 {   
  19.                     cmd.CommandText = "update T_User Set ErrorTimes=ErrorTimes+1 where UserName=@UserName";   
  20.                     cmd.Parameters.AddWithValue("UserName",txtUserName.Text);   
  21.                     cmd.ExecuteNonQuery();   
  22.                 }   
  23.             }   
  24.         }   
  25.   
  26.         private void resetErrorTimes()   
  27.         {   
  28.             using (SqlConnection conn = new SqlConnection(@"Data Source=./SQLEXPRESS;   
  29. AttachDBFilename=D:/My Documents/Documents/Visual Studio 2010/Projects/登陆练习1/登陆练习1/MyDB.mdf;   
  30. Integrated Security=True;User Instance=True"))   
  31.             {   
  32.                 conn.Open();   
  33.                 using (SqlCommand cmd = conn.CreateCommand())   
  34.                 {   
  35.                     cmd.CommandText = "update T_User Set ErrorTimes=0 where UserName=@UserName";   
  36.                     cmd.Parameters.AddWithValue("UserName", txtUserName.Text);   
  37.                     cmd.ExecuteNonQuery();   
  38.                 }   
  39.             }   
  40.         }   
  41.   
  42.         private void button1_Click(object sender, EventArgs e)   
  43.         {   
  44.             using (SqlConnection conn = new SqlConnection(@"Data Source=./SQLEXPRESS;   
  45. AttachDBFilename=D:/My Documents/Documents/Visual Studio 2010/Projects/登陆练习1/登陆练习1/MyDB.mdf;   
  46. Integrated Security=True;User Instance=True"))   
  47.             {   
  48.                 conn.Open();   
  49.                 using (SqlCommand cmd = conn.CreateCommand())   
  50.                 {   
  51.                     cmd.CommandText = "select * from T_User where UserName=@UserName";   
  52.                     cmd.Parameters.AddWithValue("UserName",txtUserName.Text);   
  53.                     using (SqlDataReader reader = cmd.ExecuteReader())   
  54.                     {   
  55.                         if (reader.Read())   
  56.                         {   
  57.                             int errortimes = reader.GetInt32(reader.GetOrdinal("ErrorTimes"));   
  58.                             if (errortimes > 3)   
  59.                             {   
  60.                                 MessageBox.Show("密码错误次数过多,禁止登陆!");   
  61.                                 return;   
  62.                             }   
  63.                             else  
  64.                             {   
  65.                                 string password = reader.GetString(reader.GetOrdinal("Password"));   
  66.                                 if(password==txtPassword.Text)   
  67.                                 {   
  68.                                     MessageBox.Show("用户登陆成功!");   
  69.                                     resetErrorTimes();   
  70.                                 }   
  71.                                 else  
  72.                                 {   
  73.                                     incErrorTimes();   
  74.                                     MessageBox.Show("密码错误!");   
  75.                                 }   
  76.                             }   
  77.                         }   
  78.                         else  
  79.                         {   
  80.                             MessageBox.Show("用户名不存在!");   
  81.                         }   
  82.                     }   
  83.                 }   
  84.             }   
  85.         }   
  86.     }   
  87. }   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值