DAL层代码:
public Users SelectUserByUserNameAndPassWordAndPowerId(string userName, string passWord,string powerId)
{
Users users = null;
string sql = "select * from Users where UserName=" + "'" + userName + "' and PassWord=" + "'" + passWord + "'and " + "'"+powerId+"'";
using (SqlDataReader reader = DBHelper.ExcuteReader(sql))
{
if (reader.Read())
{
users = new Users();
users.Id = Convert.ToInt32(reader["Id"]);
users.UserName = reader["UserName"].ToString();
users.Password = reader["PassWord"].ToString();
users.PowerId = Convert.ToInt32(reader["PowerId"]);
users.Lastlogip = reader["LastLogIp"].ToString();
users.Logtime = Convert.ToDateTime(reader["LogTime"]);
users.Isloged = Convert.ToInt32(reader["IsLoged"]);
}
return users;
}
}
BLL层:
public static bool Login(string UserName,string PassWord,string PowerId)
{
UsersDAL usersdal = new UsersDAL();
Users user = null;
user = usersdal.SelectUserByUserNameAndPassWordAndPowerId(UserName, PassWord, PowerId);
if (user==null)
{
return false;
}
else
{
return true ;
}
}
WEB层:
<tr >
<td width="100" height="40">账 号</td>
<td height="40">
<div align="left">
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="不能为空!" ControlToValidate="txtUserName"></asp:RequiredFieldValidator>
</div>
</td>
</tr>
<tr>
<td width="100" height="40">密 码</td>
<td height="40">
<div align="left">
<asp:TextBox ID="txtUserPwd" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="不能为空!" ControlToValidate="txtUserPwd"></asp:RequiredFieldValidator>
</div>
</td>
</tr>
<tr>
<td align="center" colspan="3">
<asp:DropDownList ID="RoleDropDownList" runat="server">
<asp:ListItem Value="|1|">学工处</asp:ListItem>
<asp:ListItem Value="|2|">宿舍楼管</asp:ListItem>
<asp:ListItem Value="|3|">维修人员</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="left" colspan="3" >
<a href="StuLogin.aspx" style="text-decoration:none;" >学生请点击此处</a>
<asp:Button ID="txtButton" runat="server" Text="登录" OnClick="txtButton_Click"/>
</td>
</tr>
<tr>
<td align="center" colspan="3" >
<asp:Label ID="lblmessage" runat="server" forecolor="Red"></asp:Label>
</td>
</tr>
对应的CS代码: protected void RoleDropDownList_SelectedIndexChanged()
{
if (RoleDropDownList.SelectedIndex == 1)
{
Response.Redirect("Admin/Default.aspx");
}
if (RoleDropDownList.SelectedIndex == 2)
{
Response.Redirect("Department/Default.aspx");
}
if (RoleDropDownList.SelectedIndex == 3)
{
Response.Redirect("Maintain/Default.aspx");
}
}
protected void txtButton_Click(object sender, EventArgs e)
{
string UserName = txtUserName.Text.Trim();
string PassWord = txtUserPwd.Text.Trim();
string PowerId = RoleDropDownList.Text.Trim();
bool success = UsersBLL.Login(UserName, PassWord,PowerId);
if (success)
{
RoleDropDownList_SelectedIndexChanged();
}
else
{
lblmessage.Text = "用户名或密码出错!";
}
}
public Users SelectUserByUserNameAndPassWordAndPowerId(string userName, string passWord,string powerId)
{
Users users = null;
string sql = "select * from Users where UserName=" + "'" + userName + "' and PassWord=" + "'" + passWord + "'and " + "'"+powerId+"'";
using (SqlDataReader reader = DBHelper.ExcuteReader(sql))
{
if (reader.Read())
{
users = new Users();
users.Id = Convert.ToInt32(reader["Id"]);
users.UserName = reader["UserName"].ToString();
users.Password = reader["PassWord"].ToString();
users.PowerId = Convert.ToInt32(reader["PowerId"]);
users.Lastlogip = reader["LastLogIp"].ToString();
users.Logtime = Convert.ToDateTime(reader["LogTime"]);
users.Isloged = Convert.ToInt32(reader["IsLoged"]);
}
return users;
}
}
BLL层:
public static bool Login(string UserName,string PassWord,string PowerId)
{
UsersDAL usersdal = new UsersDAL();
Users user = null;
user = usersdal.SelectUserByUserNameAndPassWordAndPowerId(UserName, PassWord, PowerId);
if (user==null)
{
return false;
}
else
{
return true ;
}
}
WEB层:
<tr >
<td width="100" height="40">账 号</td>
<td height="40">
<div align="left">
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="不能为空!" ControlToValidate="txtUserName"></asp:RequiredFieldValidator>
</div>
</td>
</tr>
<tr>
<td width="100" height="40">密 码</td>
<td height="40">
<div align="left">
<asp:TextBox ID="txtUserPwd" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="不能为空!" ControlToValidate="txtUserPwd"></asp:RequiredFieldValidator>
</div>
</td>
</tr>
<tr>
<td align="center" colspan="3">
<asp:DropDownList ID="RoleDropDownList" runat="server">
<asp:ListItem Value="|1|">学工处</asp:ListItem>
<asp:ListItem Value="|2|">宿舍楼管</asp:ListItem>
<asp:ListItem Value="|3|">维修人员</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="left" colspan="3" >
<a href="StuLogin.aspx" style="text-decoration:none;" >学生请点击此处</a>
<asp:Button ID="txtButton" runat="server" Text="登录" OnClick="txtButton_Click"/>
</td>
</tr>
<tr>
<td align="center" colspan="3" >
<asp:Label ID="lblmessage" runat="server" forecolor="Red"></asp:Label>
</td>
</tr>
对应的CS代码: protected void RoleDropDownList_SelectedIndexChanged()
{
if (RoleDropDownList.SelectedIndex == 1)
{
Response.Redirect("Admin/Default.aspx");
}
if (RoleDropDownList.SelectedIndex == 2)
{
Response.Redirect("Department/Default.aspx");
}
if (RoleDropDownList.SelectedIndex == 3)
{
Response.Redirect("Maintain/Default.aspx");
}
}
protected void txtButton_Click(object sender, EventArgs e)
{
string UserName = txtUserName.Text.Trim();
string PassWord = txtUserPwd.Text.Trim();
string PowerId = RoleDropDownList.Text.Trim();
bool success = UsersBLL.Login(UserName, PassWord,PowerId);
if (success)
{
RoleDropDownList_SelectedIndexChanged();
}
else
{
lblmessage.Text = "用户名或密码出错!";
}
}
解决方案 »
- 泛型方法的使用
- devexpress的layoutControlItem控件隐藏后怎么显示出来
- 分享一段socks5代理的代码(带用户名,密码验证)
- 关于Marshal.PtrToStructure出错的问题!!!求解答!!!
- 看看下面的代码怎样简洁.还有这个问题还有没有其它的解决方法..
- @Import URL()导入一个样式表,在ASPX页面中为什么没有效果呢?
- 小弟弄到一个商城系统,怎么主页改不了?
- 在winform的dataGrid中如何能使其没有载入数据时也能把网格显示出来
- console.read 问题;
- 为什么我的Datagrid中DataGridBoolColume类型列总是显示为null状态(灰)
- datagridview显示问题
- DataGrid 列标题
{
Users users = null;
string sql = "select * from Users where UserName=" + "'" + userName + "' and PassWord=" + "'" + passWord + "'and " + "'"+powerId+"'";----------------------------
是要根据userName,passWord,powerId来查询么? string sql = "select * from Users where UserName='" + userName + "' and PassWord='" + passWord + "'and powerId='" + powerId+"'";
检查powerId值
在查询分析器执行
public Users SelectUserByUserNameAndPassWordAndPowerId(string userName, string passWord,string powerId)
{
Users users = null;
string sql = "select * from Users where UserName=" + "'" + userName + "' and PassWord=" + "'" + passWord + "'and " + "'"+powerId+"'";
using (SqlDataReader reader = DBHelper.ExcuteReader(sql))
{
if (reader.Read())
{
users = new Users();
users.Id = Convert.ToInt32(reader["Id"]);
users.UserName = reader["UserName"].ToString();
users.Password = reader["PassWord"].ToString();
users.PowerId = Convert.ToInt32(reader["PowerId"]);
users.Lastlogip = reader["LastLogIp"].ToString();
users.Logtime = Convert.ToDateTime(reader["LogTime"]);
users.Isloged = Convert.ToInt32(reader["IsLoged"]);
}
return users;
}
}
你传值的类型错了
{
Users users = null;
string sql = "select * from Users where UserName=" + "'" + userName + "' and PassWord=" + "'" + passWord + "'and " + "'"+powerId+"'";
using (SqlDataReader reader = DBHelper.ExcuteReader(sql))
{
if (reader.Read())
{
users = new Users();
users.Id = Convert.ToInt32(reader["Id"]);
users.UserName = reader["UserName"].ToString();
users.Password = reader["PassWord"].ToString();
users.PowerId = Convert.ToInt32(reader["PowerId"]);
users.Lastlogip = reader["LastLogIp"].ToString();
users.Logtime = Convert.ToDateTime(reader["LogTime"]);
users.Isloged = Convert.ToInt32(reader["IsLoged"]);
}
return users;
}
}
{
string UserName = txtUserName.Text.Trim();
string PassWord = txtUserPwd.Text.Trim();
string PowerId = RoleDropDownList.Text.Trim();
bool success = UsersBLL.Login(UserName, PassWord,PowerId);
if (success)
{
RoleDropDownList_SelectedIndexChanged();
}
else
{
lblmessage.Text = "用户名或密码出错!";
}
}获取的值到底是个数值 还是一个字符值
既然是得到数字 string PowerId = RoleDropDownList.Text.Trim();
获取到底却是那个值
这样得到数字