基于ASP.NET的在线论坛系统开发 |
您所在的位置:网站首页 › 篮球技术论坛 › 基于ASP.NET的在线论坛系统开发 |
一、前言
本系统开发语言为C#,数据库为SQL Server,开发环境为VS2010。主要功能是为用户提供了一个发布信息和讨论问题的平台,用户分为三个级别,对应不同的操作权限。基础操作为浏览、发表、删除帖子和对帖子的评论回复。管理员可以对论坛板块进行管理,也可以删除普通用户发表的帖子。 二、系统设计 数据库设计论坛系统中主要的数据表有tbUser(用户信息表)、tbPost(帖子信息表)、tbRevert(回帖信息表)和tbModule(版块信息表)。数据库名:BBSDB。 实体层(Model)设计在论坛网站系统中,包含4个实体类,它们分别是User类(用户信息类)、Module类(版块类)、Post类(帖子类)和Revert类(回复信息类),以Post类为例,其源码如下: namespace Model { public class Post { private int postID;//帖子ID public int PostID { get { return postID; } set { postID = value; } } private string postTitle;//帖子标题 public string PostTitle { get { return postTitle; } set { postTitle = value; } } private string postContent;//帖子内容 public string PostContent { get { return postContent; } set { postContent = value; } } private int userID;//帖子的发布者 public int UserID { get { return userID; } set { userID = value; } } private DateTime postDate;//发布时间 public DateTime PostDate { get { return postDate; } set { postDate = value; } } private int moduleID;//模块ID,与Module类保持一致 public int ModuleID { get { return moduleID; } set { moduleID = value; } } } } 数据访问层(DAL)设计数据访问层主要用来执行一些数据库的操作,如连接数据库,对数据实行增加、删除、修改、查询等操作,DAL层将这些操作封装起来,并将所取得的结果返回给表现层。在论坛网站系统中,共包含5个类,它们分别是SQLHelper类、PostDAL类、UserDAL类、ModuleDAL类和RevertDAL类。其中SQLHelper类用来封装一些常用的数据库操作,其他4个类分别用来表示对数据库表的一些基本操作。现以postDAL为例,其源码如下: using Model; namespace DAL { public class PostDAL { #region 新建一个帖子 public bool CreatePost(Post post) { bool flag = false; string sqlStr = "insert into tbPost(PostTitle,PostContent,PostDate,UserID,ModuleId) values" + "(@postTitle,@postContent,@postDate,@userID,@moduleID)"; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@postTitle",post.PostTitle), new SqlParameter("@postContent",post.PostContent), new SqlParameter("@postDate",post.PostDate), new SqlParameter("@userID",post.UserID), new SqlParameter("@moduleID",post.ModuleID) }; try { SQLHelper helper = new SQLHelper(); flag = helper.ExecuteCommand(sqlStr, param); } catch { flag = false; } return flag; } #endregion #region 删除一个帖子 public bool DelPost(int postID) { bool flag = false; string sqlStr = "delete from tbPost where PostID = @postID"; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@postID",postID) }; try { SQLHelper helper = new SQLHelper(); flag = helper.ExecuteCommand(sqlStr, param); } catch { flag = false; } return flag; } #endregion #region 编辑帖子信息 public bool UpdatePost(Post post) { bool flag = false; string sqlStr = "update tbPost set PostTitle = @postTitle,PostContent = @postContent where PostID = @postID"; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@postTitle",post.PostTitle), new SqlParameter("@postContent",post.PostContent), new SqlParameter("@postID",post.PostID) }; try { SQLHelper helper = new SQLHelper(); flag = helper.ExecuteCommand(sqlStr, param); } catch { flag = false; } return flag; } #endregion #region 通过板块ID查询帖子 public DataSet GetPostByModuleID(int moduleID) { string sqlStr = "select p.PostID,m.ModuleName,p.PostTitle,p.PostContent,u.UserName,p.PostDate from tbPost p,tbModule m,tbUser u where m.ModuleID = @moduleID and p.UserID = u.UserID and p.ModuleID = m.ModuleID "; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@moduleID",moduleID) }; SQLHelper helper = new SQLHelper(); try { DataSet ds = helper.GetDataSet(sqlStr,param); if (ds.Tables[0].Rows.Count > 0 && null != ds) { return ds; } else { return null; } } catch { return null; } } #endregion #region 通过帖子ID获取帖子 public Post GetPostByPostID(int postID) { string sqlStr = "select * from tbPost where PostID = @postID"; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@postID",postID) }; SQLHelper helper = new SQLHelper(); try { DataSet ds = helper.GetDataSet(sqlStr,param); if (ds!=null && ds.Tables[0].Rows.Count> 0) { DataRow row = ds.Tables[0].Rows[0]; Post post = new Post(); post.PostID = postID; post.PostTitle = row["postTitle"].ToString(); post.PostContent = row["postContent"].ToString(); post.UserID = Convert.ToInt32(row["userID"].ToString()); post.ModuleID = Convert.ToInt32(row["moduleID"].ToString()); post.PostDate = Convert.ToDateTime(row["postDate"].ToString()); return post; } else { return null; } } catch { return null; } } #endregion #region 获取所有的帖子信息,仅管理员使用 public DataSet GetAllPosts() { string sqlStr = "select p.PostID,m.ModuleName,p.PostTitle,p.PostContent,u.UserName,p.PostDate from tbPost p,tbModule m,tbUser u where p.UserID = u.UserID and p.ModuleID = m.ModuleID "; SQLHelper helper = new SQLHelper(); try { DataSet ds = helper.GetDataSet(sqlStr); if (ds != null) { return ds; } else { return null; } } catch { return null; } } #endregion #region 根据条件查询帖子 public DataSet GetPosts(string moduleName, string userName, string postTitle) { string sqlStr = "select p.PostTitle,u.UserName,p.BuildDate from tbPost p,tbUser u,tbModule m" + "where p.UserID = u.UserID and p.ModuleID = m.ModuleID and 1=1"; if ("" != moduleName) { sqlStr += "and ModuleName = @moduleName"; } if ("" != userName) { sqlStr +="and UserName = @userName"; } if ("" != postTitle) { sqlStr += "and PostTitle like %@postTitle%"; } SqlParameter[] param = new SqlParameter[] { new SqlParameter("@moduleName",moduleName), new SqlParameter("@userName",userName), new SqlParameter("@postTitle",postTitle) }; SQLHelper helper = new SQLHelper(); try { DataSet ds = helper.GetDataSet(sqlStr, param); if (null != ds && ds.Tables[0].Rows.Count > 0) { return ds; } else { return null; } } catch { return null; } } #endregion } } 在DAL层中由SQLHelper类封装一些基本的数据库连接和交互方法,完成与数据库的直接交互,其他类通过调用该类的方法完成数据从应用程序到数据库的交互。SQLHelper的源码如下: namespace DAL { public class SQLHelper { private static SqlConnection conn; //与数据库连接 public static SqlConnection Connection { get { string sqlStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString(); if (conn == null) { conn = new SqlConnection(sqlStr); conn.Open(); } else if (conn.State == System.Data.ConnectionState.Broken) { conn.Close(); conn.Open(); } else if (conn.State == System.Data.ConnectionState.Closed) { conn.Open(); } return conn; } } //带参数查询 public DataSet GetDataSet(string sqlStr,SqlParameter[] param) { DataSet ds = new DataSet(); SqlCommand myComm = new SqlCommand(sqlStr,Connection); myComm.Parameters.AddRange(param); SqlDataAdapter myAdapter = new SqlDataAdapter(myComm); myAdapter.Fill(ds); return ds; } //不带参查询 public DataSet GetDataSet(string sqlStr) { DataSet ds = new DataSet(); SqlDataAdapter myAdapter = new SqlDataAdapter(sqlStr,Connection); myAdapter.Fill(ds); return ds; } public bool ExecuteCommand(string sqlStr) { bool flag = false; SqlCommand myComm = new SqlCommand(sqlStr,Connection); int count = myComm.ExecuteNonQuery(); if (count > 0) flag = true; return flag; } //带参数非查询 public bool ExecuteCommand(string sqlStr, SqlParameter[] param) { bool flag = false; SqlCommand myComm = new SqlCommand(sqlStr,Connection); myComm.Parameters.AddRange(param); int count = myComm.ExecuteNonQuery(); if (count > 0) flag = true; return flag; } } } 业务逻辑层(BLL)设计业务逻辑层在多层架构中,主要用来调用数据访问层中的各个操作类,它分离开了表现层和数据访问层,更好地解决了各层之间的耦合度。在BBS论坛系统中,业务逻辑层包含4个类:UserBLL类、PostBLL类、ReplayBLL类和ModuleBLL类。现以PostBLL为例,其源码如下: using DAL; using Model; using System.Data; namespace BLL { public class PostBLL { PostDAL postDAL = new PostDAL(); //发表帖子 public bool CreatePost(Post post) { return postDAL.CreatePost(post); } //删除帖子 public bool DelPost(int postID) { return postDAL.DelPost(postID); } //获取某模块下的帖子 public DataSet GetPostByModuleID(int moduleID) { return postDAL.GetPostByModuleID(moduleID); } //根据ID获取帖子 public Post GetPostByPostID(int postID) { return postDAL.GetPostByPostID(postID); } //根据条件查询帖子 public DataSet GetPosts(string moduleName,string userName,string postTitle) { return postDAL.GetPosts(moduleName,userName,postTitle); } //获取所有的帖子 public DataSet GetAllPosts() { return postDAL.GetAllPosts(); } } } 页面(WebUI 层)实现WebUI设置为启动项目,将index.aspx设置为起始页,运行程序,系统即从数据库中读取模块信息,并显示在界面上。其运行效果如图所示。 用户列表页面:该页面仅管理员可见,展示数据库中存储的所有用户的信息。管理员在该页面可以通过条件过滤查询用户信息,并且可以跳转至用户信息编辑页面更新某个用户的权限和信息以及跳转至删除用户。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |