效果展示
程序关系
App,Config
DAL
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FLogin.DAL{ ////// DAL-Student /// class Student { ////// 取得学员列表 /// ///public DataTable GetStudents() { return SqlHelper.ExecuteDataTable("SELECT * FROM dbo.Student"); } }}
using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FLogin.DAL{ class SqlHelper { ////// 对配置文件connnectionStrings节进行读取 /// static string connstr = System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; ////// 查询数据库返回一张表 /// /// /// ///public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters) { // 创建临时数据集,内存。 DataSet ds = new DataSet(); // 创建适配器对象 SqlDataAdapter adapter = new SqlDataAdapter(sql, connstr); // 给适配器对象的查询命令对象添加参数 adapter.SelectCommand.Parameters.AddRange(parameters); try { // 充填数据集 adapter.Fill(ds); return ds.Tables[0]; } catch { return null; } } /// /// 对数据库进行增,删、该等活动 /// /// /// ///public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connstr)) { using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddRange(parameters); conn.Open(); return cmd.ExecuteNonQuery(); } } } /// /// 对数据库进行查询 /// /// /// ///public static object ExecuteScalar(string sql, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connstr)) { using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddRange(parameters); conn.Open(); return cmd.ExecuteScalar(); } } } }}
using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FLogin.DAL{ class UserInfo { ////// 根据姓名取得用户,得到一行数据集 /// /// ///public DataTable GetUserByName(string _name) { return SqlHelper.ExecuteDataTable("SELECT * ,DATEDIFF(MINUTE,LastErrTime,GETDATE())FROM dbo.UserInfo WHERE UserName =@UserName", new SqlParameter("@UserName", SqlDbType.NVarChar) { Value = _name}); } /// /// 重置错误次数 /// /// ///public int ReSetErrTimes(int _id) { SqlParameter sp = new SqlParameter(); sp.ParameterName = "@id"; sp.Value = _id; return SqlHelper.ExecuteNonQuery("UPDATE dbo.UserInfo SET ErrTimes=0 WHERE id = @id", new SqlParameter("@id", SqlDbType.Int) { Value = _id}); } /// /// 更新错误时间 /// /// ///public int UpDateLastErrTime(int _id) { return SqlHelper.ExecuteNonQuery("UPDATE dbo.UserInfo SET LastErrTime = GETDATE() WHERE id = @id", new SqlParameter("@id", SqlDbType.Int) { Value = _id}); } /// /// 更新错误次数 /// /// ///public int UpDateErrTimes(int _id) { return SqlHelper.ExecuteNonQuery("update UserInfo Set ErrTimes=ErrTimes+1 where id=@id", new SqlParameter("@id", SqlDbType.Int) { Value=_id}); } }}
BLL
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FLogin.BLL{ class Student { ////// 取的学员列表 /// ///public DataTable GetStudents() { DAL.Student dal = new DAL.Student(); return dal.GetStudents(); } }}
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FLogin.BLL{ class UserInfo { ////// 判断是否登录成功!! /// /// /// ///public bool ChechLogin(string _name, string _pwd) { DAL.UserInfo dal = new DAL.UserInfo(); DataTable dt = dal.GetUserByName(_name); // 三种情况:1.账号不存在 2.密码错误 3.登录锁定 // 这个判断只能存在一个账户。 if (dt.Rows.Count != 1) { return false; } DataRow dr = dt.Rows[0]; if (Convert.ToInt32(dr[5]) < 15) { return false; } if (dr[2].ToString() == _pwd) { dal.ReSetErrTimes(Convert.ToInt32(dr[0]));// 登录成功后,重置错误次数 return true; } else { // 密码不正确 // 更新错误次数,更新数据库,dr里面的数据是在更新之前就取出来了。 dal.UpDateErrTimes(Convert.ToInt32(dr[0])); // 错误三次就更新错误时间 if (Convert.ToInt32(dr[3]) + 1 == 3) { dal.UpDateLastErrTime(Convert.ToInt32(dr[0]));//记录最后错误时间 dal.ReSetErrTimes(Convert.ToInt32(dr[0]));//重置错误次数 } return false; } } }}
Flogin
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace FLogin{ public partial class FLogin : Form { public FLogin() { InitializeComponent(); } private void btnOK_Click(object sender, EventArgs e) { // 设置当前窗口对系那个DialogResult就会关闭窗口,并且有返回值 // 前提是这个窗口是被ShowDialog出现的 // ShowDialog的返回值就是这设置的DialogResult BLL.UserInfo bll = new BLL.UserInfo(); if (bll.ChechLogin(tbName.Text, tbPassWord.Text)) { this.DialogResult = DialogResult.OK; } else { MessageBox.Show("登录失败!!"); } } private void btnSorry_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; } }}
FMain
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace FLogin{ public partial class FMain : Form { public FMain() { InitializeComponent(); } private void 学员管理ToolStripMenuItem_Click(object sender, EventArgs e) { FStu fs = new FStu(); //设置将一个窗口显示在这个窗口内部,前提,这个窗口是一个多文档窗口(isMdiContainer=true) fs.MdiParent = this; fs.Show(); } }}
FStu
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace FLogin{ public partial class FStu : Form { public FStu() { InitializeComponent(); } private void FStu_Load(object sender, EventArgs e) { BLL.Student bll = new BLL.Student(); DataTable table = bll.GetStudents(); dvgMain.DataSource = table; } }}
Program
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace FLogin{ static class Program { ////// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // 首先创建窗体对象 FLogin f1 = new FLogin(); if (f1.ShowDialog() == DialogResult.OK) { Application.Run(new FMain()); } } }}