基于ASP.NET的在线论坛系统开发
一、前言
本系統(tǒng)開發(fā)語(yǔ)言為C#,數(shù)據(jù)庫(kù)為SQL Server,開發(fā)環(huán)境為VS2010。主要功能是為用戶提供了一個(gè)發(fā)布信息和討論問(wèn)題的平臺(tái),用戶分為三個(gè)級(jí)別,對(duì)應(yīng)不同的操作權(quán)限。基礎(chǔ)操作為瀏覽、發(fā)表、刪除帖子和對(duì)帖子的評(píng)論回復(fù)。管理員可以對(duì)論壇板塊進(jìn)行管理,也可以刪除普通用戶發(fā)表的帖子。
二、系統(tǒng)設(shè)計(jì)
數(shù)據(jù)庫(kù)設(shè)計(jì)
論壇系統(tǒng)中主要的數(shù)據(jù)表有tbUser(用戶信息表)、tbPost(帖子信息表)、tbRevert(回帖信息表)和tbModule(版塊信息表)。數(shù)據(jù)庫(kù)名:BBSDB。
實(shí)體層(Model)設(shè)計(jì)
在論壇網(wǎng)站系統(tǒng)中,包含4個(gè)實(shí)體類,它們分別是User類(用戶信息類)、Module類(版塊類)、Post類(帖子類)和Revert類(回復(fù)信息類),以Post類為例,其源碼如下:
namespace Model {public class Post{private int postID;//帖子IDpublic int PostID{get { return postID; }set { postID = value; }}private string postTitle;//帖子標(biāo)題public string PostTitle{get { return postTitle; }set { postTitle = value; }}private string postContent;//帖子內(nèi)容public string PostContent{get { return postContent; }set { postContent = value; }}private int userID;//帖子的發(fā)布者public int UserID{get { return userID; }set { userID = value; }}private DateTime postDate;//發(fā)布時(shí)間public DateTime PostDate{get { return postDate; }set { postDate = value; }}private int moduleID;//模塊ID,與Module類保持一致public int ModuleID{get { return moduleID; }set { moduleID = value; }}} }數(shù)據(jù)訪問(wèn)層(DAL)設(shè)計(jì)
數(shù)據(jù)訪問(wèn)層主要用來(lái)執(zhí)行一些數(shù)據(jù)庫(kù)的操作,如連接數(shù)據(jù)庫(kù),對(duì)數(shù)據(jù)實(shí)行增加、刪除、修改、查詢等操作,DAL層將這些操作封裝起來(lái),并將所取得的結(jié)果返回給表現(xiàn)層。在論壇網(wǎng)站系統(tǒng)中,共包含5個(gè)類,它們分別是SQLHelper類、PostDAL類、UserDAL類、ModuleDAL類和RevertDAL類。其中SQLHelper類用來(lái)封裝一些常用的數(shù)據(jù)庫(kù)操作,其他4個(gè)類分別用來(lái)表示對(duì)數(shù)據(jù)庫(kù)表的一些基本操作。現(xiàn)以postDAL為例,其源碼如下:
using Model; namespace DAL {public class PostDAL{#region 新建一個(gè)帖子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 刪除一個(gè)帖子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 通過(guò)板塊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 通過(guò)帖子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 根據(jù)條件查詢帖子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類封裝一些基本的數(shù)據(jù)庫(kù)連接和交互方法,完成與數(shù)據(jù)庫(kù)的直接交互,其他類通過(guò)調(diào)用該類的方法完成數(shù)據(jù)從應(yīng)用程序到數(shù)據(jù)庫(kù)的交互。SQLHelper的源碼如下: namespace DAL {public class SQLHelper{private static SqlConnection conn; //與數(shù)據(jù)庫(kù)連接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;}}//帶參數(shù)查詢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;}//帶參數(shù)非查詢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;}} }業(yè)務(wù)邏輯層(BLL)設(shè)計(jì)
業(yè)務(wù)邏輯層在多層架構(gòu)中,主要用來(lái)調(diào)用數(shù)據(jù)訪問(wèn)層中的各個(gè)操作類,它分離開了表現(xiàn)層和數(shù)據(jù)訪問(wèn)層,更好地解決了各層之間的耦合度。在BBS論壇系統(tǒng)中,業(yè)務(wù)邏輯層包含4個(gè)類:UserBLL類、PostBLL類、ReplayBLL類和ModuleBLL類。現(xiàn)以PostBLL為例,其源碼如下:
using DAL; using Model; using System.Data; namespace BLL {public class PostBLL{PostDAL postDAL = new PostDAL();//發(fā)表帖子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);}//根據(jù)ID獲取帖子public Post GetPostByPostID(int postID){return postDAL.GetPostByPostID(postID);}//根據(jù)條件查詢帖子public DataSet GetPosts(string moduleName,string userName,string postTitle){return postDAL.GetPosts(moduleName,userName,postTitle);}//獲取所有的帖子public DataSet GetAllPosts(){return postDAL.GetAllPosts();}} }頁(yè)面(WebUI 層)實(shí)現(xiàn)
WebUI設(shè)置為啟動(dòng)項(xiàng)目,將index.aspx設(shè)置為起始頁(yè),運(yùn)行程序,系統(tǒng)即從數(shù)據(jù)庫(kù)中讀取模塊信息,并顯示在界面上。其運(yùn)行效果如圖所示。
帖子列表頁(yè)面:用戶由首頁(yè)點(diǎn)擊模塊可跳轉(zhuǎn)至帖子列表界面,該界面主要包含一個(gè)DataList控件,顯示相應(yīng)模塊下的帖子列表。在本頁(yè)可以跳轉(zhuǎn)至編輯頁(yè)面進(jìn)行回帖(需要擁有權(quán)限)。
其后臺(tái)PostList.asp.cs文件代碼主要完成調(diào)用B業(yè)務(wù)邏輯層PostBLL類的相應(yīng)方法,完成數(shù)據(jù)的帖子信息數(shù)據(jù)的綁定以及相應(yīng)前臺(tái)的請(qǐng)求,完成頁(yè)面的跳轉(zhuǎn),核心代碼如下:
用戶列表頁(yè)面:該頁(yè)面僅管理員可見,展示數(shù)據(jù)庫(kù)中存儲(chǔ)的所有用戶的信息。管理員在該頁(yè)面可以通過(guò)條件過(guò)濾查詢用戶信息,并且可以跳轉(zhuǎn)至用戶信息編輯頁(yè)面更新某個(gè)用戶的權(quán)限和信息以及跳轉(zhuǎn)至刪除用戶。
權(quán)限錯(cuò)誤頁(yè)面:該頁(yè)面是一個(gè)錯(cuò)誤處理頁(yè)面,當(dāng)用戶欲執(zhí)行其權(quán)限范圍外的操作將自動(dòng)跳轉(zhuǎn)至該頁(yè)面。該頁(yè)面顯示錯(cuò)誤提示,并使用JavaScript技術(shù)提供一個(gè)返回上一步操作的超鏈接。該界面實(shí)際上是為系統(tǒng)添加了一層容錯(cuò)機(jī)制,增加了系統(tǒng)的友好性。
總結(jié)
以上是生活随笔為你收集整理的基于ASP.NET的在线论坛系统开发的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何获得了高权重网站的高质量外链的方法?
- 下一篇: ASP.NET Core: 全新的ASP