登录事件
private void btnlogin_Click(object sender, EventArgs e) { //登录按钮事件 try { //判断最后登录时间 是不是今天 string s5 = "select * from Users where UserName=@u"; SQLiteParameter p4 = new SQLiteParameter("@u",txtUserName.Text); DataTable d= SqliteHelper.ExecuteDatatable(s5, p4); if (Convert.ToDateTime(d.Rows[0]["LastLoginTime"]).ToString("yyyy-MM-dd")!=DateTime.Now.ToString("yyyy-MM-dd")) { string s4 = "update Users set LastLoginTime=datetime('now','localtime'), LoginCount=0,ErrorCount=0 where UserName=@u"; SqliteHelper.ExecuteNonQuery(s4, p4); } if (txtPwd.Text.Trim() != "" && txtUserName.Text.Trim() != "") { string s = "select count(*) from Users where UserName=@u and PassWord=@p"; SQLiteParameter[] p = { new SQLiteParameter("@u", txtUserName.Text), new SQLiteParameter("@p", txtPwd.Text) }; int i = Convert.ToInt32(SqliteHelper.ExecuteScalar(s, p)); if (i > 0) { string s1 = "select * from Users where UserName=@u and PassWord=@p"; SQLiteParameter[] p1 = { new SQLiteParameter("@u", txtUserName.Text), new SQLiteParameter("@p", txtPwd.Text) }; DataTable dt = SqliteHelper.ExecuteDatatable(s1, p1); if (Convert.ToInt32(dt.Rows[0]["LoginCount"]) > 2) { string s2 = "update Users set Status = '禁用', LastLoginTime=datetime('now','localtime') where UserName=@u and PassWord=@p"; SqliteHelper.ExecuteNonQuery(s2, p1); MessageBox.Show("登陆次数超过3次,用户名已禁用"); txtUserName.Enabled = false; txtPwd.Enabled = false; return; } else { string s3 = "update Users set LoginCount = LoginCount+1 ,LastLoginTime=datetime('now','localtime') where UserName='" + txtUserName.Text + "' and PassWord='" + txtPwd.Text + "'"; int count = SqliteHelper.ExecuteNonQuery(s3, null); this.DialogResult = DialogResult.OK; } } else { string s2 = "update Users set ErrorCount = ErrorCount + 1,LastLoginTime=datetime('now','localtime') where UserName='" + txtUserName.Text + "'"; int count = SqliteHelper.ExecuteNonQuery(s2, null); string s1 = "select * from Users where UserName=@u"; SQLiteParameter[] p1 = { new SQLiteParameter("@u", txtUserName.Text)}; DataTable dt = SqliteHelper.ExecuteDatatable(s1, p1); if (Convert.ToInt32(dt.Rows[0]["ErrorCount"]) > 4) { MessageBox.Show("错误次数超过5次,不可登录"); } else { MessageBox.Show(("错误次数"+dt.Rows[0]["ErrorCount"].ToString()+"次")); } } } else { MessageBox.Show("用户名或密码不能为空"); } } catch (Exception ex) { MessageBox.Show("程序出现异常,异常原因:" + ex.Message); } }