C#七层登陆及代码展示

前言

一步一个脚印~
本篇文章仅为七层学习参考,熟悉七层走向,不涉及重构登陆。(缺失判断逻辑)

七层登陆

what

注意指向问题。
错误图:
包图

正确图:

正确

这个包图很清晰的表示七层在三层的基础上增加了facade,factory,IDAL层,Enitity。其实这里的enitity就代表着实体层,在三层里我们已经用过了,就是将User类封装起来。也就是这里所说的实体层。那么我们今天要学习的也就又少了一个。增加的其他三层作用是什么呢?
从图上我们可以很清晰的知道这三层也是为了减少耦合的。

  1. facade(外观层):较少U层和B层的耦合
  2. Factory(抽象工厂):实现了工厂模式+反射,使数据库的修改更加简便,通过修改配置文件实现数据库的修改
  3. IDAL(接口层):定义了一个统一的接口,减少了B层和D层的耦合
  4. Enitity(实体层):封装一些功能性代码,定义一些实体类型和实体集合用于各个层次传递参数

代码顺序

注意:先后问题。如果先写U层,它会涉及到调用,所以在没写B层之前,U层是无法完成的。写代码顺序应从箭头指向的反方向来写。
1. Enitity(实体层)
2. IDAL(接口层)
3. DAL(数据访问层)
→app配置
4. Factory(工厂层)
5. BLL(业务逻辑层)
6. Facade(外观曾)
7. UI(界面层)

代码展示

Enitity(实体层)

public class userinfo
    {
        public string UserID { get; set; }
        public string PWD { get; set; }
        public string UserName { get; set; }
        public string Level { get; set; }
        public string state { get; set; }

    }

IDAL(接口层)

using System.Data;//引用

namespace IDAL
{
    public interface LoginIDAL
    {
         DataTable selectUser(Entity.userinfo user);
    }
}

DAL(数据访问层)

配置数据库:
1

代码如下:

<appSettings>
      <add key ="connStr" value ="server=LOVQG\SQLEXPRESS;database=Login_sys;uid=sa;pwd= 123"/>
      <add key ="DB" value ="DAL"/>
    </appSettings>

DAL类:

using System.Data;  //引用
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public class LoginDAL:IDAL.LoginIDAL
    {
        public DataTable selectUser(Entity.userinfo user)
        {
            SQLHelper sqlHelper = new SQLHelper();

            //参数
            SqlParameter[] sqlparams = { new SqlParameter("@UserID", user.UserID), new SqlParameter("@PassWord", user.PWD) };
            string sql = @"SELECT * FROM [User_Info] WHERE UserID =@UserID AND PWD = @PassWord";
            DataTable table = sqlHelper.ExecuteQuery(sql, sqlparams, CommandType.Text);     //(sql, CommandType.Text, sqlparams);
            return table;

        }
    }
}

SQLHelper类:

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public class SQLHelper
    {
        private SqlConnection conn = null;
        private SqlCommand cmd = null;
        private SqlDataReader sdr = null;

        //----------------------

        public SQLHelper()
        {
        //连接
            string connStr = ConfigurationManager.AppSettings["connStr"];
            conn = new SqlConnection(connStr);
        }


        //----------------------

        private SqlConnection Getconn()   //需有返回值
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();      //打开数据库
            }
            return conn;
        }


        //---------------------

        /// <summary>
        /// 执行带参数的查询SQL语句或存储过程
        /// </summary>
        /// <param name="cmdText">查询SQL语句或存储过程</param>
        /// <param name="paras">参数集合</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>
        public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct)
        {
            DataTable dt = new DataTable();
            cmd = new SqlCommand(cmdText, Getconn());

            cmd.CommandType = ct;
            cmd.Parameters.AddRange(paras);
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }   
    }
}

Factory(工厂层)

using System.Reflection;//引用反射
using System.Configuration;

namespace Factory
{
    public class LoginFactory
    {
        //添加引用集
        string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
        public IDAL.LoginIDAL CreateUser()
        {
            //反射+工厂的应用----------添加引用using System.Reflection;

            string ClassName = StrDB + "." + "LoginDAL";//DAL的类名  StrDB+          
            return (IDAL.LoginIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);           
        }
    }
}

BLL(业务逻辑层)

using System.Data;

namespace BLL
{
    public class LoginBLL
    {
        public DataTable UserBLL(Entity.userinfo user)
        {
            Factory.LoginFactory fact = new Factory.LoginFactory();//实例化工厂
            IDAL.LoginIDAL idal = fact.CreateUser();//调用工厂方法创建接口
            DataTable table = idal.selectUser(user);//接受D层的返回值            
            return table;

        }
    }
}

Facade(外观层)

    public class LoginFacade
    {
        public DataTable SelectUser(Entity.userinfo user)
        {
            DataTable flag;
            BLL.LoginBLL UserBLL = new BLL.LoginBLL();

            flag = UserBLL.UserBLL(user);//返回值
            return flag;
        }    
    }

UI(界面层)→未判断权限~

        private void buttonOK_Click(object sender, EventArgs e)
        {
        //判空

            if (txtUserID.Text.Trim() == "") //1.判断用户名和密码是否为空,一行代码解决
            {
                MessageBox.Show("用户名不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            if (txtPassWord.Text == "")
            {
                MessageBox.Show("密码不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPassWord.Focus();
            }

            else

            {
                Entity.userinfo user = new Entity.userinfo();                //实例化用户

                user.UserID = txtUserID.Text.Trim();                         //接收信息
                user.PWD = txtPassWord.Text;                                 //

                Facade.LoginFacade FLogin = new Facade.LoginFacade();        //3.实例化外观将参数传递给外观层
                DataTable flag = FLogin.SelectUser(user);                    //4.调用外观的方法,返回给user

                #region//判断登陆
                if (flag.Rows.Count != 0)                                   //-------为什么不用布尔值,bool无法返回rowx
                {
                    MessageBox.Show("小主,您可以进来了", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();

                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    Form a = new Form();

                    a.Show();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                    txtUserID.Text = "";
                    txtPassWord.Text = "";
                    txtUserID.Focus();//获得焦点
                }
                #endregion
            }
        } 

登陆效果

1

这里写图片描述

错误集锦

错误集锦链接

后记

完成这个登陆还真是不容易啊。感谢大神们的帮助与指点~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值