使用三层的思想实现登陆

题目:做一个登陆窗口,实现登陆。登陆成功后实现修改密码的功能。在登陆过程中,判断用户名或密码是否正确,修改
密码时判断两次密码是否相等,原密码是否正确。(密码使用MD5加密算法)

要求:使用三层的思想去实现该功能。(UI,DAL,BLL)

截图如下:

      

1、DAL层程序如下:

// sqlHelper.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace 简单的三层.DAL { public class SqlHelper { private static readonly string connstr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; // 增删查改 public static int ExecNoquary(string sql, CommandType comtype, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(connstr)) { using (SqlCommand com = new SqlCommand(sql, con)) { com.CommandType = comtype; if (pms != null) { com.Parameters.AddRange(pms); } con.Open(); return com.ExecuteNonQuery(); } } } // 返回一条数据 public static object ExecSalary(string sql, CommandType comtype, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(connstr)) { using (SqlCommand com = new SqlCommand(sql, con)) { com.CommandType = comtype; if (pms != null) { com.Parameters.AddRange(pms); } con.Open(); return com.ExecuteScalar(); } } } // 返回SqlDataReader public static SqlDataReader ExectureReader(string sql, CommandType cmdType, params SqlParameter[] pms) { SqlConnection con = new SqlConnection(connstr); using (SqlCommand com = new SqlCommand(sql, con)) { com.CommandType = cmdType; if (pms != null) { com.Parameters.AddRange(pms); } try { con.Open(); return com.ExecuteReader(CommandBehavior.CloseConnection); } catch { con.Close(); con.Dispose(); throw; } } } } }

  

// T_SeatDal.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using 简单的三层.Model;

namespace 简单的三层.DAL
{
    class T_SeatDal
    {
        public int Login(string loginId, string password)
        {
            string sql = "select count(*) from T_Seats where CC_LoginId=@Id and CC_LoginPassword=@pwd";
            SqlParameter[] pms = new SqlParameter[] { 
                new SqlParameter("@Id",loginId),
                new SqlParameter("@pwd",password)
            };
            return (int)SqlHelper.ExecSalary(sql,CommandType.Text,pms);
        }

        public T_Seat GetUserBasicInfoByLogininId(string uid)
        {
            T_Seat model = null;
            string sql = "select cc_autoid,cc_loginpassword,cc_username from T_Seats where CC_LoginId=@uid";
            using (SqlDataReader reader = SqlHelper.ExectureReader(sql, System.Data.CommandType.Text, new SqlParameter("@uid",uid)))
            {
                if (reader.HasRows)
                {
                    if (reader.Read())
                    {
                        model = new T_Seat();
                        model.CC_AutoId = reader.GetInt32(0);
                        model.CC_LoginPassword = reader.GetString(1);
                        model.CC_UserName = reader.GetString(2);
                    }
                }
            }
            return model;
            // 如果没有查询到数据则返回null
        }

        // 校验旧密码
        public int CheckOldPassword(int autoid, string oldPwd)
        {
            string sql = "select count(*) from T_Seats where CC_AutoId=@autoId and CC_LoginPassword=@oldPwd";
            SqlParameter[] pms = new System.Data.SqlClient.SqlParameter[] { 
                new SqlParameter("@autoId",autoid),
                new SqlParameter("@oldPwd",oldPwd)
            };
            return (int)SqlHelper.ExecSalary(sql,CommandType.Text,pms);
        }

        //修改密码
        public int UpdatePassword(int autoId, string newPwd)
        {
            string sql = "Update T_Seats set CC_LoginPassword=@newpwd where CC_AutoId=@autoId";
            SqlParameter[] pms = new System.Data.SqlClient.SqlParameter[] { 
                new SqlParameter("@newpwd",newPwd),
                new SqlParameter("@autoId",autoId)
            };
            return SqlHelper.ExecNoquary(sql, System.Data.CommandType.Text,pms);
        }
    }
}

  

// TblPerHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace 简单的三层.DAL
{
    class TblPerHelper
    {
        public bool UpdatePerson(int autoId)
        {
            string strsql = "update Person set Age = Age+1 where ID=@id";
            SqlParameter[] pms = new SqlParameter[] { 
                new SqlParameter("@id",autoId)
            };
            return SqlHelper.ExecNoquary(strsql,CommandType.Text,pms)>0;
        }
    }
}

2、BLL层

// CommonHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace 简单的三层.BLL
{
    class CommonHelper
    {
        public static string GetMD5FormString(string msg)
        {
            // 1.创建一个用来计算MD5值的类的对象
            using (MD5 md5 = MD5.Create())
            {
                // 把字符串转换为byte[]
                // 注意如果字符中包含汉字使用utf-8编码转换为byte[],当其他地方计算MD5值的时候,如果汉字使用了不同的编码,同样的汉字生成的byte[]也是不一样的,所以计算出来的MD5值也是不一样的
                byte[] msgBuffer = Encoding.Default.GetBytes(msg);
                // 2.计算给定字符串的MD5值
                // 返回值就是计算后的MD5值,如何把一个长度为16的byte[]数组转换为一个长度为32的字符串:就是把每个byte转成16进制同时保留2位即可
                byte[] md5Buffer = md5.ComputeHash(msgBuffer);
                md5.Clear();

                StringBuilder sbMd5 = new StringBuilder();
                for (int i = 0; i < md5Buffer.Length; i++)
                {
                    sbMd5.Append(md5Buffer[i].ToString("x2"));
                }
                return sbMd5.ToString();
            }
        }
    }
}

  

//T_SeatsBll.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 简单的三层.DAL;
using 简单的三层.Model;


namespace 简单的三层.BLL
{
    class T_SeatsBll
    {
        T_SeatDal dal = new T_SeatDal();
        public bool Login(string loginId, string passWord)
        {
            return dal.Login(loginId,CommonHelper.GetMD5FormString(passWord))>0;
        }

        public loginState CheckUserLogin(string loginId,string password,out T_Seat model)
        {
            model = dal.GetUserBasicInfoByLogininId(loginId);
            if (model == null)
            {
                return loginState.UserNotExits;
            }
            else if (model.CC_LoginPassword == CommonHelper.GetMD5FormString(password))
            {
                return loginState.Success;
            }
            else
            {
                return loginState.ErrorPassWord;
            }
        }

        public ChangePasswordResult ChangePassWord(int autoId,string oldPwd,string newPwd1,string newPwd2)
        {
            // 校验两次输入的新密码
            if (CheckPassword(newPwd1, newPwd2))
            {
                // 校验旧密码
                if (CheckOldPassWord(autoId, oldPwd))
                {
                    // 如果旧密码正确,那么执行修改密码操作
                    if (dal.UpdatePassword(autoId,CommonHelper.GetMD5FormString(newPwd1)) > 0)
                    {
                        return ChangePasswordResult.Succeed;
                    }
                    else
                    {
                        return ChangePasswordResult.Faild;
                    }
                }
                else
                {
                    return ChangePasswordResult.ErrorOldPassword;
                }

            }
            else
            {
                return ChangePasswordResult.DiffNewPassword;
            }
        }

        // 校验两次新密码是否一致
        public bool CheckPassword(string new1, string new2)
        {
            return new1 == new2;
        }

        private bool CheckOldPassWord(int autoid,string oldPwd)
        {
            return dal.CheckOldPassword(autoid,CommonHelper.GetMD5FormString(oldPwd))>0;
        }
    }
    
    //修改密码的枚举
    public enum ChangePasswordResult
    { 
        DiffNewPassword,
        ErrorOldPassword,
        Succeed,
        Faild
    } 
        // 用户登陆结果
        public enum loginState
        {
            Success,
            UserNotExits,
            ErrorPassWord
        }
}

  

//TblPerBll.cs
using System;
using 简单的三层.DAL;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 简单的三层.BLL
{
    class TblPerBll
    {
        public bool IncreAge(int autoId)
        { 
            TblPerHelper per = new TblPerHelper();
            if (per.UpdatePerson(autoId))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

3.UI层

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;
using 简单的三层.BLL;
using 简单的三层.Model;

namespace 简单的三层.UI
{
    public partial class frmLogin2 : Form
    {
        public frmLogin2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 采集数据
            string uid = tB_Login.Text.Trim();
            string password = tB_Pwd.Text;

            // 调用业务逻辑数据
            T_SeatsBll bll = new T_SeatsBll();
            T_Seat model = null;
            loginState state = bll.CheckUserLogin(uid,password,out model);
            switch (state)
            {
                case loginState.Success:
                    MessageBox.Show("Ok");
                    // 记录当前用户的Id
                    GlobalHelper.auto_id = model.CC_AutoId;
                    label3.Text = model.CC_UserName;
                    btn_ChangePassWord.Enabled = true;
                    break;
                case loginState.UserNotExits:
                    MessageBox.Show("User nit exits");
                    break;
                case loginState.ErrorPassWord:
                    MessageBox.Show("Error password");
                    break;
                default:
                    break;
            }
        }
        // 点击修改密码
        private void btn_ChangePassWord_Click(object sender, EventArgs e)
        {
            frmChangePassword frm = new frmChangePassword();
            frm.Show();
        }
    }
}

  

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;
using 简单的三层.BLL;

namespace 简单的三层.UI
{
    public partial class frmChangePassword : Form
    {
        public frmChangePassword()
        {
            InitializeComponent();
        }

        private void btn_ChangePassword_Click(object sender, EventArgs e)
        {
            // 采集数据
            string old = textBox1.Text;
            string new1 = textBox2.Text;
            string new2 = textBox3.Text;

            // 调用业务逻辑层方法
            T_SeatsBll bll = new T_SeatsBll();
            ChangePasswordResult result = bll.ChangePassWord(GlobalHelper.auto_id,old,new1,new2);
            switch (result)
            {
                case ChangePasswordResult.DiffNewPassword:
                    MessageBox.Show("两次输入的新密码不一致");
                    break;
                case ChangePasswordResult.ErrorOldPassword:
                    MessageBox.Show("旧密码错误");
                    break;
                case ChangePasswordResult.Succeed:
                    MessageBox.Show("修改密码成功");
                    break;
                case ChangePasswordResult.Faild:
                    MessageBox.Show("修改密码错误");
                    break;
                default:
                    break;
            }
        }
    }
}

4、除了三层之外还有Model层,该层不在三层之内,一般它用于各层之间数据的传递

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 简单的三层.Model
{
    public class T_Seat
    {
        // 用来存储数据库表中的数据
        public int CC_AutoId { get; set; }
        public string CC_LoginId { get; set; }
        public string CC_LoginPassword { get; set; }
        public string CC_UserName { get; set; }
        public int CC_ErrorTimes { get; set; }
        public DateTime? CC_LockDataTime { get; set; }
        public int? CC_Testint { get; set; }
    }
}

  

  

posted @ 2015-11-04 16:38  JiangXiaoKun  阅读(391)  评论(0编辑  收藏  举报