commenttask.cs

来自「该项目中对 SQLHelper 类进行了简单封装」· CS 代码 · 共 194 行

CS
194
字号
/* 
 * CommentTask.cs @Microsoft Visual Studio 2005 <.NET Framework 2.0>
 * AfritXia
 * 02.02/2007
 * 
 * 评论数据库任务实现类
 * 
 */

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Text;

using NET.AfritXia.MyHome.Model.Message;
using NET.AfritXia.MyHome.DBTask.Access2000.Events;
using NET.AfritXia.MyHome.DBTask.Definition;

namespace NET.AfritXia.MyHome.DBTask.Access2000
{
	/// <summary>
	/// 评论数据库任务实现类
	/// </summary>
	public partial class CommentTask : DBTaskBase, ICommentTask
	{
		// 评论添加事件
		private event CommentEventHandler m_appendEvent;
		// 评论删除事件
		private event CommentEventHandler m_deleteEvent;

		#region ICommentTask 成员
		/// <summary>添加评论</summary>
		/// <param name="newComment">新评论信息</param>
		public void Append(Comment newComment)
		{
			MyOleDbParamCollection paramList = new MyOleDbParamCollection();

			// 设置所属文章 ID
			paramList.Add("@BelongToArticleUID", OleDbType.Integer).Value = newComment.BelongToArticleUID;
			// 发送人名称
			paramList.Add("@PostUser", OleDbType.VarWChar, 32).Value = newComment.PostUser;
			// 评论内容
			paramList.Add("@TextContent", OleDbType.LongVarWChar).Value = newComment.TextContent;
			// 客户端 IP 地址
			paramList.Add("@ClientIP", OleDbType.VarWChar, 255).Value = newComment.ClientIP;

			// 执行 SQL 语句
			CustomSQLHelper.ExecuteNoneQuery(SQL_Append, paramList.ToArray(), this.DBConn, this.DBTranx);

			// 触发评论添加事件
			this.OnCommentAppend(new CommentEvent(newComment));
		}

		/// <summary>删除评论</summary>
		/// <param name="commentID">评论信息 ID</param>
		public void Delete(int commentID)
		{
			if (commentID <= 0)
				return;

			// 获取要删除的评论
			Comment delComment = this.ViewComment(commentID);

			if (delComment == null)
				return;

			MyOleDbParamCollection paramColl = new MyOleDbParamCollection();

			// 设置评论编号
			paramColl.Add("@CommentID", OleDbType.Integer).Value = commentID;

			// 执行 SQL 查询
			CustomSQLHelper.ExecuteNoneQuery(SQL_Delete, paramColl.ToArray(), this.DBConn, this.DBTranx);

			// 触发评论删除事件
			this.OnCommentDelete(new CommentEvent(delComment));
		}

		/// <summary>
		/// 浏览评论信息
		/// </summary>
		/// <param name="parentArticleID">所属文章 ID</param>
		/// <returns></returns>
		public IList<Comment> ViewCommentList(int belongToArticleID)
		{
			if (belongToArticleID <= 0)
				return null;

			// 评论对象
			Comment comment = null;
			// 创建评论列表
			List<Comment> commentList = new List<Comment>();

			MyOleDbParamCollection paramColl = new MyOleDbParamCollection();

			// 设置文章 ID
			paramColl.Add("@ParentArticleID", OleDbType.Integer).Value = belongToArticleID;

			using (OleDbDataReader dr = CustomSQLHelper.ExecuteReader(
				SQL_ViewCommentList, paramColl.ToArray()))
			{
				while (dr.Read())
				{
					comment = CustomSQLHelper.CreateObject<Comment>(dr);

					commentList.Add(comment);
				}
			}

			return commentList;
		}
		#endregion

		/// <summary>
		/// 浏览单个评论信息
		/// </summary>
		/// <param name="commentID"></param>
		/// <returns></returns>
		public Comment ViewComment(int commentID)
		{
			if (commentID <= 0)
				return null;

			Comment comment = null;

			MyOleDbParamCollection paramColl = new MyOleDbParamCollection();

			// 设置评论 ID
			paramColl.Add("@CommentUID", OleDbType.Integer).Value = commentID;

			using (OleDbDataReader dr = CustomSQLHelper.ExecuteReader(
				SQL_ViewComment, paramColl.ToArray()))
			{
				if (dr.Read())
					comment = CustomSQLHelper.CreateObject<Comment>(dr);
			}

			return comment;
		}

		/// <summary>
		/// 添加或移除评论添加事件处理句柄
		/// </summary>
		public event CommentEventHandler AppendEvent
		{
			add
			{
				this.m_appendEvent += value;
			}

			remove
			{
				this.m_appendEvent -= value;
			}
		}

		/// <summary>
		/// 添加或移除评论删除事件处理句柄
		/// </summary>
		public event CommentEventHandler DeleteEvent
		{
			add
			{
				this.m_deleteEvent += value;
			}

			remove
			{
				this.m_deleteEvent -= value;
			}
		}

		/// <summary>
		/// 触发评论添加事件
		/// </summary>
		/// <param name="CommentEvent"></param>
		public virtual void OnCommentAppend(CommentEvent args)
		{
			if (this.m_appendEvent != null)
				this.m_appendEvent(this, args);
		}

		/// <summary>
		/// 触发评论删除事件
		/// </summary>
		/// <param name="CommentEvent"></param>
		public virtual void OnCommentDelete(CommentEvent args)
		{
			if (this.m_deleteEvent != null)
				this.m_deleteEvent(this, args);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?