简单评论系统开发过程(一)

本人第一次写日志,见笑了,写得不好的地方请多指教~~开始步入正题~~(不然什么东东都飞过来了咧~~)
1、开发手记
  这几天休息,没事做,就想上网看看新闻,发现每家新闻后面都有发表评论,有大有小,心里自己写个玩,于是乎就参照“网易”的评论系统写了个简单的评论。
2、需求分析
  发表评论,支持和反对,支持对各种标题的评论,表中有个临时ID,可存入相关标题的ID值,从而关联获取值。
3、数据库设计
   这里比较简单,就一张表,如下:

数据库名:comment

comment

序号

列名

数据类型

长度

小数位

标识

主键

允许空

默认值

字段说明

1

comm_id

int

4

0

 

 

评论ID

2

comm_tid

int

4

0

 

 

 

评论标题ID

3

comm_name

nvarchar

40

0

 

 

 

评论人

4

comm_time

datetime

8

3

 

 

 

发布时间

5

comm_ip

nvarchar

40

0

 

 

 

评论人IP

6

comm_agree

int

4

0

 

 

(0)

赞同

7

comm_against

int

4

0

 

 

(0)

反对

8

comm_audit

int

4

0

 

 

(0)

审核

9

comm_hot

int

4

0

 

 

(0)

精华

10

comm_hits

int

4

0

 

 

(0)

阅读次数

11

comm_content

ntext

16

0

 

 

 

评论内容


4、程序设计
  程序三层开发(BS架构),每一层代码马上贴出。
  ⑴数据层
   使用了微软的数据类(SqlHelper.cs),这个类的代码我就不贴出了。
   数据外理类

SqlDal.cs

  1using System;
  2using System.Data;
  3using System.Collections.Generic;
  4using System.Data.SqlClient;
  5using System.Text;
  6using Model;
  7using System.Collections;
  8namespace DAL
  9{
 10   public class SqlDal
 11    {
 12        /// <summary>
 13        /// 添加一条新记录
 14        /// </summary>
 15        /// <param name="comm"></param>

 16        public void Insert(comment comm)
 17        {
 18            try
 19            {
 20                StringBuilder sql = new StringBuilder();
 21                sql.Append(" insert into comment(");
 22                sql.Append("comm_tid, comm_name, comm_time, comm_ip, comm_agree, comm_against, comm_audit, comm_hot, comm_hits, comm_content");
 23                sql.Append(")");
 24                sql.Append(" values(");
 25                sql.Append("" + comm.Comm_tid + ",");
 26                sql.Append("'" + comm.Comm_name + "',");
 27                sql.Append("'" + comm.Comm_time + "',");
 28                sql.Append("'" + comm.Comm_ip + "',");
 29                sql.Append("" + comm.Comm_agree + ",");
 30                sql.Append("" + comm.Comm_against + ",");
 31                sql.Append("" + comm.Comm_audit + ",");
 32                sql.Append("" + comm.Comm_hot + ",");
 33                sql.Append("" + comm.Comm_hits + ",");
 34                sql.Append("'" + comm.Comm_content + "'");
 35                sql.Append("");
 36                SqlHelper.ExecuteNonQuery(sql.ToString());
 37            }

 38            catch (Exception ex)
 39            {
 40                throw new Exception(ex.Message, ex);
 41            }

 42        }

 43       /// <summary>
 44       /// 更新一条现有记录
 45       /// </summary>
 46       /// <param name="comm"></param>

 47       public void Update(comment comm)
 48       {
 49           try
 50           {
 51               StringBuilder sql = new StringBuilder();
 52               sql.Append(" update comment set ");
 53               sql.Append("comm_tid=" + comm.Comm_tid + ",");
 54               sql.Append("comm_name='" + comm.Comm_name + "',");
 55               sql.Append("comm_time='" + comm.Comm_time + "',");
 56               sql.Append("comm_ip='" + comm.Comm_ip + "',");
 57               sql.Append("comm_agree=" + comm.Comm_agree + ",");
 58               sql.Append("comm_against=" + comm.Comm_against + ",");
 59               sql.Append("comm_audit=" + comm.Comm_audit + ",");
 60               sql.Append("comm_hot=" + comm.Comm_hot + ",");
 61               sql.Append("comm_hits=" + comm.Comm_hits + ",");
 62               sql.Append("comm_content='" + comm.Comm_content + "'");
 63               sql.Append(" where comm_id=" + comm.Comm_id);
 64               SqlHelper.ExecuteNonQuery(sql.ToString());
 65           }

 66           catch (Exception ex)
 67           {
 68               throw new Exception(ex.Message, ex);
 69           }

 70       }

 71       /// <summary>
 72       /// 删除一条现有记录
 73       /// </summary>
 74       /// <param name="comm_id"></param>

 75       public void Delete(int comm_id)
 76       {
 77           StringBuilder sql = new StringBuilder();
 78           sql.Append(" delete from comment where comm_id=" + comm_id);
 79           SqlHelper.ExecuteNonQuery(sql.ToString());
 80       }

 81       /// <summary>
 82       /// 处理一条信息(如审核,推荐,热门,回收,还原)
 83       /// </summary>
 84       /// <param name="comm_id"></param>

 85       public void Deal(string str,int val,int comm_id)
 86       {
 87           StringBuilder sql = new StringBuilder();
 88           sql.Append(" update comment set " + str + "=" + val + " where comm_id=" + comm_id);
 89           SqlHelper.ExecuteNonQuery(sql.ToString());
 90       }

 91       /// <summary>
 92       /// 更新叠加
 93       /// </summary>
 94       /// <param name="str"></param>
 95       /// <param name="comm_id"></param>
 96       /// <returns></returns>

 97       public int Hits(string str,int comm_id)
 98       {
 99           StringBuilder sql = new StringBuilder();
100           sql.Append(" update comment set " + str + "=" + str + "+1 where comm_id=" + comm_id);
101           return (int)SqlHelper.ExecuteNonQuery(sql.ToString());
102       }

103       /// <summary>
104       /// 获取字段值
105       /// </summary>
106       /// <param name="str"></param>
107       /// <param name="comm_id"></param>
108       /// <returns></returns>

109       public string FieldName(string str,int comm_id)
110       {
111           string tempstr = string.Empty;
112           StringBuilder sql = new StringBuilder();
113           sql.Append("select " + str + " from comment where comm_id=" + comm_id);
114           SqlDataReader dr = SqlHelper.ExecuteReader(sql.ToString());
115           if (dr.Read())
116           {
117               tempstr = dr[0].ToString();
118           }

119           dr.Close();
120           return tempstr;
121       }

122       /// <summary>
123       /// 查询一条记录
124       /// </summary>
125       /// <param name="comm_id"></param>
126       /// <returns></returns>

127       public comment Query(int comm_id,string conditions)
128       {
129           try
130           {
131               StringBuilder sql = new StringBuilder();
132               sql.Append(" select comm_name, comm_time, comm_ip, comm_agree, comm_against, comm_audit, comm_hot, comm_hits, comm_content, comm_tid from comment ");
133               sql.Append("where 1=1 " + conditions);
134               sql.Append(" and comm_id=" + comm_id);
135               DataTable dt = SqlHelper.Query(sql.ToString()).Tables[0].Copy();
136               comment comm = new comment();
137               comm.Comm_id = comm_id;
138               if (dt.Rows.Count > 0)
139               {
140                   comm.Comm_name = dt.Rows[0]["comm_name"].ToString().Trim();
141                   comm.Comm_tid = int.Parse(dt.Rows[0]["comm_tid"].ToString().Trim());
142                   comm.Comm_time = DateTime.Parse(dt.Rows[0]["comm_time"].ToString());
143                   comm.Comm_ip = dt.Rows[0]["comm_ip"].ToString().Trim();
144                   comm.Comm_agree = int.Parse(dt.Rows[0]["comm_agree"].ToString());
145                   comm.Comm_against = int.Parse(dt.Rows[0]["comm_against"].ToString());
146                   comm.Comm_hot = int.Parse(dt.Rows[0]["comm_hot"].ToString());
147                   comm.Comm_hits = int.Parse(dt.Rows[0]["comm_hits"].ToString());
148                   comm.Comm_audit = int.Parse(dt.Rows[0]["comm_audit"].ToString());
149                   comm.Comm_content = dt.Rows[0]["comm_content"].ToString().Trim();
150                   return comm;
151               }

152               else
153               {
154                   return null;
155               }

156           }

157           catch (Exception ex)
158           {
159               throw new Exception(ex.Message, ex);
160           }

161       }

162       /// <summary>
163       /// 记录总数提取
164       /// </summary>
165       /// <returns></returns>

166       public int Total(string tablename,string str,string conditions)
167       {
168           StringBuilder sql=new StringBuilder();
169           sql.Append("select count("+str+") from " + tablename + "");
170           sql.Append(" where 1=1 " + conditions);
171           object int_total = (object)SqlHelper.ExecuteScalar(sql.ToString());
172           return (int)int_total;
173       }

174       /// <summary>
175       /// 数据分页列表提取
176       /// </summary>
177       /// <param name="startindex"></param>
178       /// <param name="pagesize"></param>
179       /// <returns></returns>

180       public DataSet CommList(string tablename,int startindex,int pagesize,string conditions)
181       {
182           StringBuilder sql = new StringBuilder();
183           sql.Append("select * from "+tablename+" ");
184           sql.Append(" where 1=1 " + conditions);
185           sql.Append(" order by comm_time desc");
186           return SqlHelper.QueryPage(SqlHelper.connstr,CommandType.Text,sql.ToString(),startindex,pagesize,null);
187       }

188       /// <summary>
189       /// 是否存在记录
190       /// </summary>
191       /// <param name="tableName"></param>
192       /// <param name="conditions"></param>
193       /// <returns></returns>

194       public bool Exists(string tablename, string conditions)
195       {
196           StringBuilder sql = new StringBuilder();
197           try
198           {
199               sql.Append(" select count(1) from " + tablename + " ");
200               sql.Append(" where 1=1 " + conditions);
201               object obj = SqlHelper.ExecuteScalar(sql.ToString());
202               if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)) || (int.Parse(obj.ToString()) == 0))
203               {
204                   return false;
205               }

206               else
207               {
208                   return true;
209               }

210           }

211           catch (Exception ex)
212           {
213               throw new Exception(ex.Message, ex);
214           }

215       }

216    }

217}

218

 待续……

posted on 2007-05-17 11:35  平平兄  阅读(181)  评论(0编辑  收藏  举报