BLL层:
LoginState.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
public enum LoginState
{///用户名不存在
NoName,
/// <summary>
/// 密码错误
/// </summary>
PwdErr,
/// <summary>
/// 登陆成功
/// </summary>
OK
}
}
StudentBll.cs
using System.Text;
using System.Threading.Tasks;
using DAL;
using Model;
namespace BLL
{
public class StudentBll
{
private StudentDal dal = new StudentDal();
public List<Student> GetAllStudent()
{
return dal.GetAllStudent().Count > 0 ? dal.GetAllStudent() : null;
}
public bool AddStudent(Student stu)
{
return dal.AddStudent(stu)>0;
}
public bool SelectCount(string StuNum)
{
return dal.SelectCount(StuNum) > 0;
}
}
}
UserInfoBll.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using Model;
namespace BLL
{
public class UserInfoBll
{
private UserInfoDal dal = new UserInfoDal();
public LoginState CheckLogin(string UserName,string Pwd)
{
UserInfo user = dal.SelectUserInfoLogin(UserName);
if(user==null)
{
return LoginState.NoName;
}
else
{
if (user.Pwd != Pwd)
{
return LoginState.NoName;
}
else
{
return LoginState.OK;
}
}
}
}
}
DAl层:
SqlHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Configuration;
namespace DAL
{
public class SqlHelper
{//在引用里添加System.Configuration引用集,数据集
private static string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
public static int ExcuteNonQuery(string sql, params SqlParameter[] pms)
{
int count = 0;
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
count = cmd.ExecuteNonQuery();
}
}
return count;
}
public static object ExcuteScalar(string sql, params SqlParameter[] pms)
{
// int count;
using (SqlConnection con = new SqlConnection(conStr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
con.Open();
return cmd.ExecuteScalar();
}
}
}
public static SqlDataReader ExcuteReader(string sql, params SqlParameter[] pms)
{
SqlConnection con = new SqlConnection(conStr);
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
try
{
con.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception)
{
con.Close();
con.Dispose();
throw;
}
}
}
public static DataTable ExcuteDataTable(string sql, params SqlParameter[] pms)
{
DataTable dt = new DataTable();
using (SqlDataAdapter sda = new SqlDataAdapter(sql, conStr))//外侧代码-using
{
if (pms != null)
{
sda.SelectCommand.Parameters.AddRange(pms);
}
sda.Fill(dt);
}
return dt;
}
}
}
StudentDal.cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace DAL
{
public class StudentDal
{
public int AddStudent(Student stu)
{
string sql = "insert into Student values(@stuNum,@stuName,@stuClass,@subject,@stuAge,@stuPhone,@stuGender)";
SqlParameter[] prm = {
new SqlParameter("@stuNum",stu.StuNum),
new SqlParameter("@stuName",stu.StuName),
new SqlParameter("@stuClass",stu.StuClass),
new SqlParameter("@subject",stu.subject),
new SqlParameter("@stuAge",stu.StuAge),
new SqlParameter("@stuPhone",stu.StuPhone),
new SqlParameter("@stuGender",stu.StuGender)
};
int count = SqlHelper.ExcuteNonQuery(sql, prm);
return count;
}
public int SelectCount(string StuNum)
{
string sql = "select count(*) from Student where StuNum=@StuNum";
SqlParameter spm = new SqlParameter("@StuNum", StuNum);
int count = Convert.ToInt32(SqlHelper.ExcuteScalar(sql, spm));
return count;
}
public List<Student> GetAllStudent()
{
List<Student> stuList = new List<Student>();
string sql = "select * from Student";
using (SqlDataReader sdr = SqlHelper.ExcuteReader(sql))
{
if (sdr.HasRows)
{
while (sdr.Read())
{
Student stu = new Student();
stu.ID = sdr.GetInt32(0);
stu.StuNum = sdr.GetString(1);
stu.StuName = sdr.GetString(2);
stu.StuClass = sdr.GetString(3);
stu.subject = sdr.GetString(4);
stu.StuAge = Convert.IsDBNull(sdr[5])?null:(byte?)sdr.GetByte(5);
//stu.StuAge = sdr.GetInt32(5);
stu.StuPhone = Convert.IsDBNull(sdr[5]) ? null : sdr.GetString(6);
stu.StuGender = Convert.IsDBNull(sdr[5]) ? null : sdr.GetString(7);
stuList.Add(stu);
}
}
}
return stuList;
}
}
}
UserInfoDal.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data.SqlClient;
namespace DAL
{
public class UserInfoDal
{
public UserInfo SelectUserInfoLogin(string UserName)
{
UserInfo user = null;
string sql = "select * from UserInfo where Name=@Name";
SqlParameter spm = new SqlParameter("@Name", UserName);
using (SqlDataReader sdr = SqlHelper.ExcuteReader(sql,spm))
{
if (sdr.Read())
{
user = new UserInfo();
user.ID = sdr.GetInt32(0);
user.Name = sdr.GetString(1);
user.Pwd = sdr.GetString(2);
}
}
return user;
}
}
}
Model层:
Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
public class Student
{
public int ID { get; set; }
public string StuNum { get; set; }
public string StuName { get; set; }
public string StuClass { get; set; }
public string subject { get; set; }
public byte? StuAge { get; set; }
public string StuPhone { get; set; }
public string StuGender { get; set; }
}
}
UserInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
public class UserInfo
{
public int ID { get; set; }
public string Name { get; set; }
public string Pwd { get; set; }
}
}
UI层:
AddStuInfo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddStuInfo.aspx.cs" Inherits="UI.AddStuInfo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
.auto-style1 {}
.auto-style2 {
height: 20px;
}
.auto-style3 {
height: 20px;
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
添加学生信息</div>
<table style="width:100%;">
<tr>
<td class="auto-style2">学号</td>
<td class="auto-style2">
<asp:TextBox ID="tb_stuNum" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>姓名</td>
<td>
<asp:TextBox ID="tb_stuName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>班级</td>
<td>
<asp:TextBox ID="tb_stuClass" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>科目</td>
<td>
<asp:TextBox ID="tb_subject" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>年龄</td>
<td>
<asp:TextBox ID="tb_stuAge" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>电话</td>
<td>
<asp:TextBox ID="tb_stuPhone" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style1" rowspan="2">性别</td>
<td class="auto-style1">
<asp:RadioButton ID="rb_Male" runat="server" Checked="True" GroupName="gender" Text="男" />
<asp:RadioButton ID="rb_Female" runat="server" GroupName="gender" Text="女" />
</td>
</tr>
<tr>
<td>
<asp:RadioButtonList ID="rbl_Gender" runat="server">
<asp:ListItem Selected="True">男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="auto-style3" colspan="2">
<asp:Button ID="bt_AddStuInfo" runat="server" OnClick="bt_AddStuInfo_Click" Text="添加" />
<input id="bt_Clear" type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
AddStuInfo.aspx.cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
namespace UI
{
public partial class AddStuInfo : System.Web.UI.Page
{
StudentBll bll = new StudentBll();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void bt_AddStuInfo_Click(object sender, EventArgs e)
{
string stuNum = tb_stuNum.Text.Trim();
string stuName = tb_stuName.Text.Trim();
string stuClass = tb_stuClass.Text.Trim();
string subject = tb_subject.Text.Trim();
if (String.IsNullOrEmpty(stuNum) || String.IsNullOrEmpty(stuName) || String.IsNullOrEmpty(stuClass) || String.IsNullOrEmpty(subject))
{
Response.Write("<script>alert('学号,姓名,班级和科目都不能为空,请重新输入!');</script>");
}
else
{
byte stuAge = 0;
Byte.TryParse(tb_stuAge.Text.Trim(), out stuAge);
string stuPhone = tb_stuPhone.Text.Trim();
string stuGender = rbl_Gender.SelectedValue == "男" ? "男" : "女";
Student stu = new Student();
stu.StuNum = stuNum;
stu.StuName = stuName;
stu.StuClass = stuClass;
stu.subject = subject;
stu.StuAge = stuAge;
stu.StuPhone = stuPhone;
stu.StuGender = stuGender;
if (bll.AddStudent(stu))
{
Response.Write("<script>alert('学生信息添加成功!');</script>");
Response.Redirect("ListStudents.aspx");
}
else
{
Response.Write("<script>alert('学生信息添加失败!')</script>");
}
}
}
}
}
ListStudents.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
using DAL;
using System.Text;
namespace UI
{
public partial class ListStudents : System.Web.UI.Page
{
private StudentBll bll = new StudentBll();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] == null)
{
Response.Redirect("Login.aspx");
}
else
{
List<Student> stuList = bll.GetAllStudent();
StringBuilder sb = new StringBuilder();
sb.Append("<table><tr><th>编号</th><th>学号</th><th>姓名</th><th>班级</th><th>科目</th><th>年龄</th><th>电话</th><th>性别</th><th>操作</th></tr>");
int index = 1;
foreach(var item in stuList)
{
sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td><a href='ShowDetailHandler.ashx?id={8}'>详情</a><a href='EditHandler.ashx?id={8}'>修改</a></td></tr>",index,item.StuNum,item.StuName,item.StuClass,item.subject,item.StuAge,item.StuPhone,item.StuGender,item.ID);
index++;
}
sb.Append("<tr><td><a href='AddStuInfo.aspx'>添加</a></td><td>...</td><td>...</td></tr>");
sb.Append("</table>");
Response.Write(sb.ToString());
}
}
}
}
Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="UI.Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>
<asp:TextBox ID="tb_UserName" runat="server"></asp:TextBox>
</div>
<asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
<asp:TextBox ID="tb_Pwd" runat="server"></asp:TextBox>
<p>
<asp:Button ID="bt_Login" runat="server" OnClick="bt_Login_Click" Text="登录" />
</p>
</form>
<p>
</p>
</body>
</html>
Login.aspx.cs
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DAL;
using Model;
namespace UI
{
public partial class Login : System.Web.UI.Page
{
private UserInfoBll bll = new UserInfoBll();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void bt_Login_Click(object sender, EventArgs e)
{
string userName = tb_UserName.Text.Trim();
string pwd = tb_Pwd.Text.Trim();
if(String.IsNullOrEmpty(userName)||String.IsNullOrEmpty(pwd))
{
Response.Write("用户名和密码不能为空,请重新输入!");
}
else
{
switch (bll.CheckLogin(userName, pwd))
{
case LoginState.NoName:
Response.Write("用户名不存在,请重新输入!");
break;
case LoginState.PwdErr:
Response.Write("密码错误,请重新输入!");
break;
case LoginState.OK:
Response.Write("登录成功!");
Session["UserName"] = userName;
Response.Redirect("ListStudents.aspx");
break;
default:
break;
}
}
}
}
}
Web.config
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="conStr" connectionString="server=.;uid=sa;pwd=123456;database=ASP_NET"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>