创建用户表的Sql语句,数据库为SQL Server2000:
1.用户登录功能的实现:
已经使用LiTianPing的动软代码生成器生成了三层架构,以下是添加的代码。
(1)DAL层的User.cs中添加两个方法:
}
}
}
}
return userInfo;
}
/// <summary>
/// 根据用户名获得用户实体
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public HJMS.Model.User GetModelByUserName(string userName)
{
StringBuilder strSql = new StringBuilder();
strSql.Append(" select top 1 UserID,UserName,UserPwd,UserCode,UserRole,CreatTime,UpdateTime,Define1,Define2 from User");
strSql.Append(" where UserName=@UserName");
SqlParameter[] pt = {new SqlParameter("@UserName", SqlDbType.VarChar)};
pt[0].Value = userName;
HJMS.Model.User user = new Model.User();
DataTable dtUser = DbHelperSQL.Query(strSql.ToString(), pt).Tables[0];
if (dtUser.Rows.Count > 0)
{
if (dtUser.Rows[0]["UserID"].ToString() != "")
{
user.UserID = int.Parse(dtUser.Rows[0]["UserID"].ToString());
}
user.UserName = dtUser.Rows[0]["UserName"].ToString();
user.UserPwd = dtUser.Rows[0]["UserPwd"].ToString();
user.UserCode = dtUser.Rows[0]["UserCode"].ToString();
user.UserRole = dtUser.Rows[0]["UserRole"].ToString();
user.CreatTime = Convert.ToDateTime(dtUser.Rows[0]["CreateTime"]);
user.UpdateTime = Convert.ToDateTime(dtUser.Rows[0]["UpdateTime"]);
user.Define1 = dtUser.Rows[0]["Define1"].ToString();
user.Define2 = dtUser.Rows[0]["Define2"].ToString();
return user;
}
else
{
return null;
}
}
(2) BLL层的User.cs中添加两个方法:
(3)UI层就是用户界面,假设Login.aspx如下的话:
// --></mce:script>
</head>
<body onload="SetFocus()" style="background-color: White;" mce_style="background-color: White;">
<form id="form1" runat="server">
<table cellspacing="0" cellpadding="3" border="0" align="center" width="530px" style="margin-top: 200px;" mce_style="margin-top: 200px;">
<tr>
<td style="width: 525px; height: 310px; background: url(images/background/loginbackground2.jpg) no-repeat;
vertical-align: top;">
<div style="text-align: right; margin-right: 20px;" mce_style="text-align: right; margin-right: 20px;">
<div style=" text-align: left;width: 218px; height: 19px; margin-top:15px; margin-right:20px">
<asp:Label ID="lblTitle" runat="server" Text="户籍管理系统" Font-Bold="true" Font-Size="Small" Font-Names="宋体"></asp:Label>
</div></div>
<div id="userloginbox" style="margin: 0px 30px;" mce_style="margin: 0px 30px;">
<table cellpadding="5px;" width="450px">
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td align="right" width="50%">
<asp:Label ID="Label2" runat="server">用户名:</asp:Label>
</td>
<td>
<asp:TextBox ID="txtUserID" runat="server" Width="150px" MaxLength="10" class="username"
EnableTheming="false"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label3" runat="server">密 码:</asp:Label>
</td>
<td>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password" Width="150px" MaxLength="20"
class="password" EnableTheming="false"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnLogin" runat="server" Text="登 录" OnClick="btnLogin_Click"></asp:Button>
</td>
</tr>
<tr>
<td colspan="2" style="height: 20px;">
<asp:Label ID="lblMessage" runat="server" EnableViewState="False"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" align="right">
IE (<asp:Label ID="lblVersion" runat="server" Text="版本 6.0"></asp:Label>)
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<br />
<br />
</form>
</body>
</html>
那么Login.aspx.cs页面就可以这样: