C#三层登录小例子

三层包图:

C#三层登录小例子_第1张图片

表示层UI,业务逻辑层BLL,数据访问层DAL,和实体层Entity。

类图:

UI层的类图是FrmLogin.

C#三层登录小例子_第2张图片

BLL层的类图是Login

C#三层登录小例子_第3张图片

DAL层的类图是UserDAO

C#三层登录小例子_第4张图片

Entity层的类图是UserInfo

C#三层登录小例子_第5张图片

序列图:

C#三层登录小例子_第6张图片

登录界面:

C#三层登录小例子_第7张图片

UI层代码:

public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.Trim();
            string password = txtPassword.Text;
            
            try
            {
                Login.BLL.LoginManager mgr = new Login.BLL.LoginManager();
                Login.Model.UserInfo user = mgr.UserLogin(userName, password);
                MessageBox.Show("登录用户:" + user.UserName);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message.ToString() );
            }         

        }


BLL层代码:

  

    public class LoginManager
    {
        public Login .Model .UserInfo  UserLogin(string userName, string password)
        {
           
            Login.DAL.UserDAO uDao = new Login.DAL.UserDAO();
            if (userName == null)
            {
                throw new Exception("请输入用户名!");                
            }

            if (password == null)
            {
                throw new Exception("请输入密码!");
            }

            Login .Model .UserInfo user= uDao.SelectUser(userName, password);

            if (user != null)
            {
                return user;
            }
            else
            {
                throw new Exception("登录失败!");
            }
        }
    }


DAL层代码:

    public class UserDAO
    {
        public static string ConnString = @"Server=XIAOZUO-PC;Database=Login;User ID =sa;Password=123456";
        public Login .Model .UserInfo  SelectUser(string userName, string password)
        {            
            using (SqlConnection conn = new SqlConnection(ConnString))   
            {
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText =@"select ID,UserName,Password,Email from Users where UserName=@UserName and Password=@Password";
                
                cmd .CommandType =CommandType .Text ;
                cmd.Parameters.Add(new SqlParameter("@UserName", userName));
                cmd.Parameters.Add(new SqlParameter("@Password", password));

                conn.Open();
                SqlDataReader reader=cmd.ExecuteReader();
                
                Login.Model .UserInfo user=null;
                while (reader .Read ())
                {
                    if (user ==null)
                    {
                        user =new Login.Model .UserInfo ();
                    }
                    user .ID =reader .GetInt32 (0);
                    user .UserName =reader.GetString (1);
                    user.Password =reader.GetString (2);
                    if (reader .IsDBNull (3))
                    {
                        user.Email =reader.GetString (3);
                    }
                }
                return user ;
            }
        }

    }


Entity层代码:

    public class UserInfo
    {
        public int ID { get; set; }
        public  string UserName {get;set;}
        public string Password {get;set;}
        public string Email {get;set;}

    }


 

你可能感兴趣的:(C#三层登录小例子)