登录页面:
引用:
DAL层引用model层,BLL层引用DAL和model层,ul层引用BLL和model层。
model层:
public int Id { get; set; } --id
public string Name { get; set; } --姓名
public string Pwd { get; set; } --密码
public int Role { get; set; } --角色
DAL层:
public static UserInfo Select(string name, string pwd)
{
//构建查询语句 = 不能like
var sql = $"select * from UserInfo where name='{name}' and pwd='{pwd}'";
var table = SqlHelper.Query(sql);
//如果没有数据 null
if (table == null || table.Rows.Count < 1)
{
return null;
}
//如果有数据,取第一行,转换为对象
var row = table.Rows[0];
UserInfo user = new UserInfo()
{
Id = Convert.ToInt32(row["Id"]),
Name = Convert.ToString(row["Name"]),
Role = Convert.ToInt32(row["Role"])
};
return user;
}
BLL层:
public static UserInfo Select(string name, string pwd)
{
return UserInfoDAL.Select(name, pwd);
}
ul层:
<dl>
<dd>账号</dd>
<dt>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></dt>
</dl>
<dl>
<dd>密码</dd>
<dt>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox></dt>
</dl>
<dl>
<dd></dd>
<dt>
<asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" />
</dt>
</dl>
protected void btnLogin_Click(object sender, EventArgs e)
{
var user = UserInfoBLL.Select(txtName.Text, txtPwd.Text);
if (user == null)
{
Response.Write("<script>alert('账号或密码不正确')</script>");
}
else if (user.Role == 0)
{
//用户
}
else if (user.Role == 1)
{
//管理员
}