DAL引用Models文件,BLL引用DAL和Models文件,主文件WebApplication1引用Bll和Models
3、根据数据库中的列写Models下的XueshengModels类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Models
public class XueshengModels
private string id;
public string Id
get { return id; }
set { id = value; }
private string name;
public string Name
get { return name; }
set { name = value; }
private string date;
public string Date
get { return date; }
set { date = value; }
4、DAL下的DBHelper(对数据库进行操作)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace DAL
public class DBHelper
public static string connstr = "server=.;database=Xuesheng;uid=sa;pwd=123123";
public static SqlConnection conn = null;
public static void Connect() {
if (conn==null)
conn = new SqlConnection(connstr);
conn.Close();
conn.Open();
public static bool NoQuery(string sql) {
Connect();
SqlCommand cmd = new SqlCommand(sql,conn);
int temp= cmd.ExecuteNonQuery();
return temp > 0;
public static SqlDataReader Reader(string sql)
Connect();
SqlCommand cmd = new SqlCommand(sql, conn);
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
5、DAL数据访问层下的service文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace DAL
public class service
public static List<Models.XueshengModels> Cha() {
List<Models.XueshengModels> list = new List<Models.XueshengModels>();
string sql = "select * from Xuesheng";
SqlDataReader read= DBHelper.Reader(sql);
while (read.Read())
Models.XueshengModels model=new Models.XueshengModels();
model.Id = read["Id"].ToString();
model.Name = read["Name"].ToString();
model.Date = read["Date"].ToString();
list.Add(model);
return list;
public static bool jia(string name) {
string chuan = string.Format("insert xuesheng values('{0}',GETDATE())",name);
if (DBHelper.NoQuery(chuan))
return true;
return false;
public static List<Models.XueshengModels> sou(string soutext)
List<Models.XueshengModels> list = new List<Models.XueshengModels>();
string sql = string.Format("select * from xuesheng where Name like '%{0}%'",soutext);
SqlDataReader read = DBHelper.Reader(sql);
while (read.Read())
Models.XueshengModels model = new Models.XueshengModels();
model.Id = read["Id"].ToString();
model.Name = read["Name"].ToString();
model.Date = read["Date"].ToString();
list.Add(model);
return list;
6、BLL业务逻辑层下调用DAL的文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
public class BllManager
public static List<Models.XueshengModels> Cha() {
return DAL.service.Cha();
public static bool jia(string name) {
return DAL.service.jia(name);
public static List<Models.XueshengModels> sou(string soutext) {
return DAL.service.sou(soutext);
7、ui表现层主界面前端部分
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!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>
<form id="form1" runat="server">
<a href="Tianjia.aspx"> 添加</a><br />
<asp:Label ID="Label1" runat="server" Text=a"搜索"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="确定" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Id" FooterText="id" HeaderText="id" />
<asp:BoundField DataField="Name" FooterText="name" HeaderText="name" />
<asp:BoundField DataField="Date" FooterText="date" HeaderText="date" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
8、ui表现层主界面后端部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
public partial class WebForm1 : System.Web.UI.Page
public void Page_Load(object sender, EventArgs e)
List<Models.XueshengModels> list= BLL.BllManager.Cha();
GridView1.DataSource = list;
GridView1.DataBind();
protected void Button1_Click(object sender, EventArgs e)
string soutex = TextBox1.Text;
List<Models.XueshengModels> list = BLL.BllManager.sou(soutex);
GridView1.DataSource = list;
GridView1.DataBind();
9、ui表现层添加界面前端部分
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Tianjia.aspx.cs" Inherits="WebApplication1.Tianjia" %>
<!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>
<form id="form1" runat="server">
<a href="WebForm1.aspx">添加</a>
<asp:Label ID="Label1" runat="server" Text="添加"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br /> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" style="height: 21px" />
</form>
</body>
</html>
10、ui表现层添加界面后端部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
public partial class Tianjia : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void Button1_Click(object sender, EventArgs e)
string text = TextBox1.Text.ToString();
bool pd=BLL.BllManager.jia(text);
if (pd)
ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('成功了!'),location.href='WebForm1.aspx'", true);
ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('失败了!')", true);
“三层架构实现校园BBS其他功能”任务描述
以上几次任务我们了解了“三层架构”的编程理念,为“校园BBS”系统搭建了三层架构的开发模式,并且以用户登录为例来讲解了三层架构是如何具体实现登录功能的,由此及彼,接下来我们的任务就是根据需求分析对“校园BBS”系统里其他功能来进行“三层架构”编程方法的实现。
校园BBS
“三层架构实现校园BBS其他功能”知识要点
“校园BBS”其他功能实现
“校园BBS”系统主要功能模块:
“三层架构实现校园BBS其他功能”知识要点
“校园BBS”其他功能实现
“校园BBS”系统功能描述
1.概要功能:用户注册,用户登录,首页显示精华帖,搜索帖子。
2.注册用户功能:修改个人信息;修改密码;发布帖子,管理个人帖子,发表评论,管理个人评论。
3.管理员功能:管理用户,修改个人信息,修改密码,管理论坛版块,审核帖子,管理帖子,管理评论,发表帖子,管理个人帖子,管理个人评论。
“三层架构实现校园BBS其他功能”知识要点
“校园BBS”其他功能实现
// 连接字符串
private readonly string connectionString = "Data Source=.;Initial Catalog=TestDB;Integrated Security=True";
// 根据用户名查询用户
public bool CheckUser(string username)
using (var conn = new SqlConnection(connectionString))
var cmd = new SqlCommand("SELECT COUNT(*) FROM Users WHERE username=@username", conn);
cmd.Parameters.AddWithValue("@username", username);
conn.Open();
return ((int)cmd.ExecuteScalar() > 0);
// 添加用户
public bool AddUser(string username, string password)
using (var conn = new SqlConnection(connectionString))
var cmd = new SqlCommand("INSERT INTO Users(username,password) VALUES(@username,@password)", conn);
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
conn.Open();
return (cmd.ExecuteNonQuery() > 0);
// 根据用户名和密码查询用户
public bool Login(string username, string password)
using (var conn = new SqlConnection(connectionString))
var cmd = new SqlCommand("SELECT COUNT(*) FROM Users WHERE username=@username AND password=@password", conn);
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
conn.Open();
return ((int)cmd.ExecuteScalar() > 0);
上述代码中,我们使用 ADO.NET 提供的 `SqlConnection` 和 `SqlCommand` 类对数据库进行操作,其中 `CheckUser` 方法用于检查用户名是否已存在,`AddUser` 方法用于添加新用户,`Login` 方法用于验证用户登录。
接下来,我们定义一个业务逻辑层(BLL),用于对数据访问层进行封装,提供更加友好的 API 接口:
```csharp
using DAL;
namespace BLL
public class UserBll
private readonly UserDal dal = new UserDal();
// 检查用户名是否已存在
public bool CheckUser(string username)
return dal.CheckUser(username);
// 注册新用户
public bool Register(string username, string password)
if (CheckUser(username))
return false; // 用户名已存在
return dal.AddUser(username, password);
// 用户登录
public bool Login(string username, string password)
return dal.Login(username, password);
在业务逻辑层中,我们调用数据访问层提供的方法进行操作,同时可以对返回结果进行处理,提供更加友好的 API 接口。
最后,我们定义一个 ASP.NET 页面,实现用户登录和注册的功能:
```csharp
using BLL;
namespace WebApplication1
public partial class Login : System.Web.UI.Page
private readonly UserBll bll = new UserBll();
protected void Page_Load(object sender, EventArgs e)
// 注册按钮点击事件
protected void btnRegister_Click(object sender, EventArgs e)
string username = txtUsername.Text.Trim();
string password = txtPassword.Text.Trim();
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
lblMsg.Text = "用户名和密码不能为空!";
return;
if (bll.Register(username, password))
lblMsg.Text = "注册成功,请登录!";
lblMsg.Text = "用户名已存在,请重新输入!";
// 登录按钮点击事件
protected void btnLogin_Click(object sender, EventArgs e)
string username = txtUsername.Text.Trim();
string password = txtPassword.Text.Trim();
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
lblMsg.Text = "用户名和密码不能为空!";
return;
if (bll.Login(username, password))
lblMsg.Text = "登录成功!";
lblMsg.Text = "用户名或密码错误,请重新输入!";
在 ASP.NET 页面中,我们调用业务逻辑层提供的方法进行操作,同时通过 `Page_Load` 方法初始化页面。需要注意的是,为了防止 SQL 注入攻击,我们应该对用户输入进行过滤和验证。此外,我们还需要将数据库连接字符串存储在配置文件中,以便在不同环境下进行配置。