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

首先把架子搭好,如下图

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

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);
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值