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

用三层架构写实现用户登录(ASP.NET)

2012-12-16 00:46 344 查看
首先把架子搭好,如下图





然后一层一层的写,先写模型层,例如:

public class User

{

private string userId;

public string UserId

{

get { return userId; }

set { userId = value; }

}

private string userPwd;

public string UserPwd

{

get { return userPwd; }

set { userPwd = value; }

}

private string userName;

public string UserName

{

get { return userName; }

set { userName = value; }

}

private string userSex;

public string UserSex

{

get { return userSex; }

set { userSex = value; }

}

private string userTel;

public string UserTel

{

get { return userTel; }

set { userTel = value; }

}

private string userAddress;

public string UserAddress

{

get { return userAddress; }

set { userAddress = value; }

}

private string userEmail;

public string UserEmail

{

get { return userEmail; }

set { userEmail = value; }

}

private string userImg;

public string UserImg

{

get { return userImg; }

set { userImg = value; }

}

}

然后就是数据链路层,例如:

public class User_DAL

{ public object CheckLogin(User user, string right)

{

string sqlText;

if (right == "管理员")

{

sqlText = "select userPwd from user_tb where userId=@userId";

}

else if (right == "部门经理")

{

sqlText = "select empPwd from Employee_tb where empId=@userId and empType='部门经理'";

}

else if (right == "CR员工")

{

sqlText = "select empPwd from Employee_tb where empId=@userId and empType='CR员工'";

}

else if (right == "司机")

{

sqlText = "select empPwd from Employee_tb where empId=@userId and empType='司机'";

}

else if (right == "操作员工")

{

sqlText = "select empPwd from Employee_tb where empId=@userId and empType='操作员工'";

}

else

{

sqlText = "select cusPwd from Customer_tb where cusId=@userId ";

}

SqlParameter[] paras = new SqlParameter[1]

{

new SqlParameter("userId",user.UserId)

};

object obj = SqlLink.GetScaler(sqlText, paras);

return obj;

}



然后就是业务逻辑层;例如:

public class User_BLL

{

User_DAL userdal = new User_DAL();

public int CheckLogin(User user, string right)

{

object obj = userdal.CheckLogin(user, right);

int result = -1;

if (obj == null)

{

result = 0; //用户不存在

}

else if (obj.ToString().Trim() == user.UserPwd)

{

result = 1;//正确

}

else if (obj.ToString().Trim() != user.UserPwd)

{

result = 2;//密码错误

}

else

{

result = 3; //系统错误

}

return result;

}

}

最后是表示层;例如:

protected void BtnLogin_Click(object sender, EventArgs e)

{

try

{

//验证登录

if (txtUserId.Text == "" || txtUserPwd.Text == "")

{

Response.Write("<script>alert('用户名或者密码不能为空')</script>");

return;

}

string userId = txtUserId.Text.Trim();

string userPwd = txtUserPwd.Text.Trim();

string right = cobRight.Text.Trim();

user.UserId= userId.Trim();

user.UserPwd = userPwd.Trim();

int i =userbll.CheckLogin(user, right);

if (i == 0)

{

Response.Write("<script>alert('用户名不存在')</script>");

}

else if (i == 1)

{

Session["userId"] = txtUserId.Text;

Session["userPwd"] = txtUserPwd.Text;

Server.Transfer("Index.aspx");

}

else if (i == 2)

{

Response.Write("<script>alert('密码错误')</script>");

}

else if (i == 3)

{

Response.Write("<script>alert('系统错误')</script>");

}

}

catch (Exception ex)

{

Response.Write(ex.Message);

}

表示层只写了方法,因为需要据具体需要更改名称。如有需要,敬请留言,有求必应。
本文出自 “Love-JS” 博客,请务必保留此出处http://leileiaijishu.blog.51cto.com/6286126/1090948
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: