您的位置:首页 > 编程语言 > C#

七层登录——C#

2016-02-27 21:09 453 查看
三层登录完成之后,紧接着就是七层登录,七层相对三层多的是设计模式、接口,目的无非就是希望解耦合,符合开闭原则,系统更加优化。刚开始的时候七层很是不理解,不知道层层之间的联系,花了好几天的时间才实现,现在不能说非常明白,但是懂的多了,我想说:只要去做,时间花在哪里都是值得的!



UI层

private void btnOK_Click(object sender, EventArgs e)
        {
            
            string UserName = txtUserName.Text.Trim();//赋值过程
            string Password = txtPassword.Text;
            if (txtUserName .Text ==string .Empty )//验证是否为空
            {
                MessageBox.Show("请输入用户名!", "登录");
            }
            if (txtPassword.Text ==string .Empty )
            {
                MessageBox.Show("请输入密码!", "登录");
            }

            JF.Facade.LoginFacade FLogin = new JF.Facade.LoginFacade();//实例化外观
            JF.Entity.LoginEntity user = FLogin.SelectUser(UserName, Password);//调用外观的方法,返回给user

            if (user!=null )
            {
                MessageBox.Show("登陆成功!");
                this.Hide  ();
                this.DialogResult =System .Windows .Forms .DialogResult .OK ;
                frmMain frmMain=new frmMain ();
                frmMain.Show();
            }
            else 
            {
                MessageBox .Show("密码或者用户名错误");
            }
        }
Facade层

public JF .Entity .LoginEntity SelectUser(string UserName,string Password)
       {
           JF.Entity.LoginEntity user = new JF.Entity.LoginEntity();//实例化实体
           JF.BLL.LoginBLL loginBll = new JF.BLL.LoginBLL();//实例化B层
           user = loginBll.SelectUser(UserName, Password);
           return user;
       }
BLL层

public JF.Entity.LoginEntity SelectUser(string UserName,string Password)
        {
            JF.Entity.LoginEntity user = new JF.Entity.LoginEntity();//实例化实体
            JF.Factory.LoginFactory Datacess = new JF.Factory.LoginFactory();//实例化工厂
            JF.IDAL.LoginIDAL ILogin;//定义接口
            ILogin = Datacess.SelectUser();//工厂中方法的返回值为接口类型 ,用接口接收
            user = ILogin.GetUser(UserName, Password);//调用接口的方法
            return user;
        }
Factory层

public static readonly string strDB = System.Configuration.ConfigurationManager.AppSettings ["DBString"];//读取配置文件
        public JF.IDAL .LoginIDAL SelectUser()//工厂里面为什么返回值是接口类型的
        {
            return (JF.IDAL.LoginIDAL)Assembly.Load(strDB).CreateInstance("JF.DAL.LoginDAO");
        }
IDAL层

JF.Entity.LoginEntity GetUser(string UserName, string Password);
DAL层

public JF.Entity.LoginEntity GetUser(string UserName, string Password)
        {
            using (SqlConnection conn = new SqlConnection(DBUtil.conString)) //通过参数DBUtil.conString打开连接数据
            {
                SqlCommand cmd = conn.CreateCommand();//创建cmd执行sql语句

                cmd.CommandText = @"select * from JFUser_Info where UserID=@UserName  and Pwd=@Password";
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add(new SqlParameter("@UserName", Password));//Parameters添加参数
                cmd.Parameters.Add(new SqlParameter("@Password", UserName));
                conn.Open();//打开数据源
                SqlDataReader reader = cmd.ExecuteReader();//读取数据并且得到结果

                JF.Entity .LoginEntity user = null;
                while (reader.Read())
                {
                    if (user == null)
                    {
                        user = new JF.Entity.LoginEntity();

                    }

                    user.UserName = reader.GetString(1);
                    user.Pwd = reader.GetString(2);
                }
                return user;
                }
}
Entity层

public class LoginEntity
    {
        private static string _userid;
        public static string UserID
        {
            get { return _userid; }
            set { _userid = value; }
        }
        private string _level;
        public string Level
        {
            get { return _level; }
            set { _level = value; }
        }
        private string _head;
        public string Head
        {
            get { return _head; }
            set { _head = value; }
        }
        private string _username;
        public string UserName
        {
            get { return _username; }
            set { _username = value; }
        }
        private string _pwd;
        public string Pwd
        {
            get { return _pwd; }
            set { _pwd = value; }
        }
    }


只要理清思路,代码实现就不是问题了,机房第一步登录,向下一步进军!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: