一个三层的复习
ui中加入dal,bll,model ;dal中引用model,bll中引用dal,model
一、ui层:
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace UI { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FormLogin formLogin = new FormLogin(); //注意这个地方要用这个写法,具体为何不用直接调用,原因忘记了,查到在补上 if (formLogin.ShowDialog() == DialogResult.OK) { Application.Run(new FormMain()); } } } }
b. 消息插件代码:
View Code
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; /// <summary> /// 消息条回调函数委托 /// </summary> public delegate void DGMsgDiv(); /// <summary> /// 消息条类 带Timer计时 /// </summary> public class MsgDiv : System.Windows.Forms.Label { private Timer timerLable = new Timer(); /// <summary> /// 消息回调 委托对象 /// </summary> private DGMsgDiv dgCallBack = null; #region 计时器 /// <summary> /// 计时器 /// </summary> public Timer TimerMsg { get { return timerLable; } set { timerLable = value; } } #endregion #region MsgDiv构造函数 /// <summary> /// MsgDiv构造函数 /// </summary> public MsgDiv() { InitallMsgDiv(7, 7); } /// <summary> /// MsgDiv构造函数 /// </summary> /// <param name="x">定位x轴坐标</param> /// <param name="y">定位y轴坐标</param> public MsgDiv(int x, int y) { InitallMsgDiv(x, y); } #endregion #region 初始化消息条 /// <summary> /// 初始化消息条 /// </summary> private void InitallMsgDiv(int x, int y) { this.AutoSize = true; this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192))))); this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; //this.ContextMenuStrip = this.cmsList; this.Font = new System.Drawing.Font("宋体", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.ForeColor = System.Drawing.Color.Red; this.Location = new System.Drawing.Point(x, y); this.MaximumSize = new System.Drawing.Size(980, 525); this.Name = "msgDIV"; this.Padding = new System.Windows.Forms.Padding(7); this.Size = new System.Drawing.Size(71, 31); this.TabIndex = 1; this.Text = "消息条"; this.Visible = false; //给委托添加事件 this.DoubleClick += new System.EventHandler(this.msgDIV_DoubleClick); this.MouseLeave += new System.EventHandler(this.msgDIV_MouseLeave); this.MouseHover += new System.EventHandler(this.msgDIV_MouseHover); this.timerLable.Interval = 1000; this.timerLable.Tick += new System.EventHandler(this.timerLable_Tick); } #endregion #region 将消息条添加到指定容器上 /// <summary> /// 将消息条添加到指定容器上Form /// </summary> /// <param name="form"></param> public void AddToControl(Form form) { form.Controls.Add(this); } /// <summary> /// 将消息条添加到指定容器上GroupBox /// </summary> /// <param name="form"></param> public void AddToControl(GroupBox groupBox) { groupBox.Controls.Add(this); } /// <summary> /// 将消息条添加到指定容器上Panel /// </summary> /// <param name="form"></param> public void AddToControl(Panel panel) { panel.Controls.Add(this); } #endregion //--------------------------------------------------------------------------- #region 消息显示 的相关参数们 hiddenClick,countNumber,constCountNumber /// <summary> /// 当前显示了多久的秒钟数 /// </summary> int hiddenClick = 0; /// <summary> /// 要显示多久的秒钟数 可变参数 /// </summary> int countNumber = 3; /// <summary> /// 要显示多久的秒钟数 固定参数 /// </summary> int constCountNumber = 3; #endregion #region 计时器 显示countNumber秒钟后自动隐藏div -timerLable_Tick(object sender, EventArgs e) private void timerLable_Tick(object sender, EventArgs e) { if (hiddenClick > countNumber - 2) { MsgDivHidden(); } else { hiddenClick++; //RemainCount(); } } #endregion #region 隐藏消息框 并停止计时 +void MsgDivHidden() /// <summary> /// 隐藏消息框 并停止计时 /// </summary> public void MsgDivHidden() { this.Text = ""; this.Visible = false; this.hiddenClick = 0; //this.tslblRemainSecond.Text = ""; if (this.timerLable.Enabled == true) this.timerLable.Stop(); //调用 委托 然后清空委托 if (dgCallBack != null && dgCallBack.GetInvocationList().Length > 0) { dgCallBack(); dgCallBack -= dgCallBack; } } #endregion #region 在消息框中显示消息字符串 +void MsgDivShow(string msg) /// <summary> /// 在消息框中显示消息字符串 /// </summary> /// <param name="msg">要显示的字符串</param> public void MsgDivShow(string msg) { this.Text = msg; this.Visible = true; this.countNumber = constCountNumber;//默认设置显示秒数为10; this.hiddenClick = 0;//重置倒数描述 this.timerLable.Start(); } #endregion #region 在消息框中显示消息字符串 并在消息消失时 调用回调函数 +void MsgDivShow(string msg, DGMsgDiv callback) /// <summary> /// 在消息框中显示消息字符串 并在消息消失时 调用回调函数 /// </summary> /// <param name="msg">要显示的字符串</param> /// <param name="callback">回调函数</param> public void MsgDivShow(string msg, DGMsgDiv callback) { MsgDivShow(msg); dgCallBack = callback; } #endregion #region 在消息框中显示消息字符串 并在指定时间消息消失时 调用回调函数 +void MsgDivShow(string msg, int seconds, DGMsgDiv callback) /// <summary> /// 在消息框中显示消息字符串 并在消息消失时 调用回调函数 /// </summary> /// <param name="msg">要显示的字符串</param> /// <param name="seconds">消息显示时间</param> /// <param name="callback">回调函数</param> public void MsgDivShow(string msg, int seconds, DGMsgDiv callback) { MsgDivShow(msg, seconds); dgCallBack = callback; } #endregion #region 在消息框中显示消息字符串,并指定消息框显示秒数 +void MsgDivShow(string msg, int seconds) /// <summary> /// 在消息框中显示消息字符串,并指定消息框显示秒数 /// </summary> /// <param name="msg">要显示的字符串</param> /// <param name="seconds">消息框显示秒数</param> public void MsgDivShow(string msg, int seconds) { this.Text = msg; this.Visible = true; this.countNumber = seconds; this.hiddenClick = 0;//重置倒数描述 this.timerLable.Start(); } #endregion //--------------------------------------------------------------------------- #region 事件们~~~! msgDIV_MouseHover,msgDIV_MouseLeave,msgDIV_DoubleClick //当鼠标停留在div上时 停止计时 private void msgDIV_MouseHover(object sender, EventArgs e) { if (this.timerLable.Enabled == true) this.timerLable.Stop(); } //当鼠标从div上移开时 继续及时 private void msgDIV_MouseLeave(object sender, EventArgs e) { //当消息框正在显示、回复框没显示、计时器正停止的时候,重新启动计时器 if (this.Visible == true && this.timerLable.Enabled == false) this.timerLable.Start(); } //双击消息框时关闭消息框 private void msgDIV_DoubleClick(object sender, EventArgs e) { MsgDivHidden(); } #endregion }
c.登陆窗口:
View Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace UI { public partial class FormLogin : Form { public FormLogin() { InitializeComponent(); } BLL.Person bll = new BLL.Person(); /// <summary> /// 登录 /// </summary> private void btnLogin_Click(object sender, EventArgs e) { string strName = txtName.Text.Trim(); string strPwd = txtPwd.Text.Trim(); try { //调用业务层对象 的登录方法 ,获得登录用户实体对象 MODEL.Person model = bll.Login(strName, strPwd, rdoStu.Checked ? 1 : 2); if (model != null) { CommonHelper.loginedUser = model;//将当前登录用户实体对象保存在静态变量中 msgDiv.MsgDivShow("登录成功啦~~:)", Success); } else { msgDiv.MsgDivShow("登录失败啦~~:)"); } } catch (Exception ex) { msgDiv.MsgDivShow(ex.Message); } } public void Success() { this.DialogResult = System.Windows.Forms.DialogResult.OK; } } }
d.主窗体:
View Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace UI { public partial class FormMain : Form { public FormMain() { InitializeComponent(); } } }
e.配置文件:
View Code
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="connStr" connectionString="server=.;database=SIM;uid=sa;pwd=oumind123"/> </connectionStrings> </configuration>
二、model层
a.
三、bll层:
a.person:
View Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BLL { /// <summary> /// 业务层 -- 人员数据操作类 /// </summary> public class Person { //创建 全局变量 -- 人员表数据操作类对象 DAL.Person dal = new DAL.Person(); #region 登录 /// <summary> /// 登录 /// </summary> /// <param name="uName">登录名</param> /// <param name="uPwd">登录密码</param> /// <param name="uType">类型</param> /// <returns>登录实体对象</returns> public MODEL.Person Login(string uName, string uPwd, int uType) { //调用数据层对象方法 查询 登录用户,并返回登录用户实体对象 MODEL.Person model = dal.GetModelByName(uName); if (model != null) { //如果 用户密码或类型错误,则将实体设置为null if (model.PPwd != uPwd || model.PType != uType) { model = null; } } return model; } #endregion } }
四、dal层:
a.
Person
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Text; namespace DAL { /// <summary> /// 数据层 人员表 操作类 /// </summary> public class Person { #region 01.根据登录名 查询用户 +MODEL.Person GetModelByName /// <summary> /// 根据登录名 查询用户 /// </summary> /// <param name="strName">登录名</param> /// <returns>MODEL.Person</returns> public MODEL.Person GetModelByName(string strName) { MODEL.Person model = null; string strSql = "select * from Person where PLoginName =@PLoginName"; //查询数据库 放回 数据表对象 DataTable dt = SqlHelper.ExcuteDataTable(strSql, new SqlParameter("PLoginName", strName)); if (dt.Rows.Count > 0) { model = new MODEL.Person();//准备一个 空的 人员表实体类对象 DataRow dr = dt.Rows[0];//获得第一行 LoadEntityData(ref model, dr);//将行数据 存入 实体对象 } return model; } #endregion #region 将 数据行dr 的每一列的数据 存入 实体对象model中对应的 属性中 /// <summary> /// 将 数据行dr 的每一列的数据 存入 实体对象model中对应的 属性中 /// </summary> /// <param name="model">实体对象</param> /// <param name="dr">DataRow</param> private void LoadEntityData(ref MODEL.Person model, DataRow dr) { if (!dr.IsNull("PID")) { model.PID = Convert.ToInt32(dr["PID"]); } if (dr["PCID"].ToString() != "") { model.PCID = int.Parse(dr["PCID"].ToString()); } if (dr["PType"].ToString() != "") { model.PType = int.Parse(dr["PType"].ToString()); } model.PLoginName = dr["PLoginName"].ToString(); model.PCName = dr["PCName"].ToString(); model.PPYName = dr["PPYName"].ToString(); model.PPwd = dr["PPwd"].ToString(); if (dr["PGender"].ToString() != "") { model.PGender = bool.Parse(dr["PGender"].ToString()); } model.PEmail = dr["PEmail"].ToString(); model.PAreas = dr["PAreas"].ToString(); if (dr["PIsDel"].ToString() != "") { model.PIsDel = bool.Parse(dr["PIsDel"].ToString()); } if (dr["PAddTime"].ToString() != "") { model.PAddTime = DateTime.Parse(dr["PAddTime"].ToString()); } } #endregion } }
SqHelper
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace DAL { /// <summary> /// 数据库连接帮助类 /// </summary> public class SqlHelper { //从配置文件获得连接字符串 private static string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; #region 01.获得 数据表对象 +DataTable ExcuteDataTable(string strSql, params SqlParameter[] paras) /// <summary> /// 获得 数据表对象 /// </summary> /// <param name="strSql">要执行的查询命令</param> /// <param name="paras">查询命令的参数数组</param> /// <returns>查询到的数据表对象</returns> public static DataTable ExcuteDataTable(string strSql, params SqlParameter[] paras) { DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(connStr)) { //创建适配器对象(卡车,它会自动开关连接通道) SqlDataAdapter da = new SqlDataAdapter(strSql, conn); //设置 适配器 对象 的查询参数 da.SelectCommand.Parameters.AddRange(paras); da.Fill(dt); } return dt; } #endregion #region 02.执行增删改 +int ExcuteNonQuery(string strSql, params SqlParameter[] paras) /// <summary> /// 获得 数据表对象 /// </summary> /// <param name="strSql">要执行的查询命令</param> /// <param name="paras">查询命令的参数数组</param> /// <returns>查询到的数据表对象</returns> public static int ExcuteNonQuery(string strSql, params SqlParameter[] paras) { int res = 0; using (SqlConnection conn = new SqlConnection(connStr)) { //创建命令对象(工人) using (SqlCommand cmd = new SqlCommand(strSql, conn)) { cmd.Parameters.AddRange(paras); conn.Open();//开启连接通道(工人需要自己开门) try { //执行增删改 res = cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } } } return res; } #endregion #region 03.执行查询单个单元格 +int ExcuteScalar(string strSql, params SqlParameter[] paras) /// <summary> /// 获得 数据表对象 /// </summary> /// <param name="strSql">要执行的查询命令</param> /// <param name="paras">查询命令的参数数组</param> /// <returns>查询到的数据表对象</returns> public static int ExcuteScalar(string strSql, params SqlParameter[] paras) { int res = -1; using (SqlConnection conn = new SqlConnection(connStr)) { //创建命令对象(工人) using (SqlCommand cmd = new SqlCommand(strSql, conn)) { cmd.Parameters.AddRange(paras); conn.Open();//开启连接通道(工人需要自己开门) try { //查询单个单元格数据 object o = cmd.ExecuteScalar(); if (o != null) res = Convert.ToInt32(o); } catch (Exception ex) { throw ex; } } } return res; } #endregion } }