基于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;//帖子IDpublic 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設置為起始頁,運行程序,系統即從數據庫中讀取模塊信息,并顯示在界面上。其運行效果如圖所示。
帖子列表頁面:用戶由首頁點擊模塊可跳轉至帖子列表界面,該界面主要包含一個DataList控件,顯示相應模塊下的帖子列表。在本頁可以跳轉至編輯頁面進行回帖(需要擁有權限)。
其后臺PostList.asp.cs文件代碼主要完成調用B業務邏輯層PostBLL類的相應方法,完成數據的帖子信息數據的綁定以及相應前臺的請求,完成頁面的跳轉,核心代碼如下:
用戶列表頁面:該頁面僅管理員可見,展示數據庫中存儲的所有用戶的信息。管理員在該頁面可以通過條件過濾查詢用戶信息,并且可以跳轉至用戶信息編輯頁面更新某個用戶的權限和信息以及跳轉至刪除用戶。
權限錯誤頁面:該頁面是一個錯誤處理頁面,當用戶欲執行其權限范圍外的操作將自動跳轉至該頁面。該頁面顯示錯誤提示,并使用JavaScript技術提供一個返回上一步操作的超鏈接。該界面實際上是為系統添加了一層容錯機制,增加了系統的友好性。
總結
以上是生活随笔為你收集整理的基于ASP.NET的在线论坛系统开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何获得了高权重网站的高质量外链的方法?
- 下一篇: 测试显卡的软件叫游戏什么,众多显卡监测软