OnePiece之Asp.Net菜鸟也来做开发(五)

YY部分仅供娱乐,请勿拍砖。正文部分,如有错误或不足,欢迎拍砖、指正。

第四章:DAL层设计及实现

呃,春哥保佑。最近俗事缠身,这个项目一直没有做得完,现在总算完成了个大概了。(呃,当然只是新闻模块)由于近期事情比较多,这个自己练手的项目就先把新闻模块做个模样出来,以后再继续了。

话说我们已经把数据库设计好了。系统的功能也基本有个大概的理解了,下面就开始编码吧。

DAL层主要实现对数据库的操作。写的时候有点偷懒,不好的地方大家指正。

SQLHelper类(由于代码本身有注释,这里就不多做解释了)

 

ContractedBlock.gif ExpandedBlockStart.gif SQLHelper
  1ExpandedBlockStart.gifContractedBlock.gif/**//*
  2*创建人:Cat_Lee
  3*创建时间:2009/8/21 10:35:38
  4*说明:数据库助手类
  5*版权所有: Cat_Lee 
  6*欢迎访问我的Blog:http://www.cnblogs.com/cat-lee/
  7*/

  8using System;
  9using System.Collections.Generic;
 10using System.Linq;
 11using System.Text;
 12using System.Data;
 13using System.Data.SqlClient;
 14using System.Configuration; 
 15
 16namespace DAL
 17ExpandedBlockStart.gifContractedBlock.gif{
 18    public class SQLHelper
 19ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 20        private SqlConnection sqlconn;
 21        private SqlDataReader sqldr;
 22        private SqlCommand sqlcmd;
 23        public SQLHelper()
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 25            sqlcmd = new SqlCommand();
 26            sqlconn = new SqlConnection();
 27        }

 28ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 29        /// 取得数据库连接
 30        /// </summary>

 31        private void Connect()
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 33            sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString();
 34            sqlconn.Open();
 35        }

 36ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 37        /// 执行不带Sql参数的数据库增、删、改操作
 38        /// </summary>
 39        /// <param name="cmd">Sql命令</param>
 40        /// <param name="ct">Sql命令类型</param>
 41        /// <returns>命令是否执行成功</returns>

 42        public bool ExcuteNonQuery(string cmd,CommandType ct)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 44            int res;
 45            this.Connect();
 46            sqlcmd.CommandText = cmd;
 47            sqlcmd.Connection = sqlconn;
 48            sqlcmd.CommandType = ct;
 49            try
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 51                res = sqlcmd.ExecuteNonQuery();
 52            }

 53            catch (Exception ex)
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 55                throw (ex);
 56            }

 57            finally
 58ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 59                if (sqlconn.State == ConnectionState.Open)
 60                    sqlconn.Close();
 61            }

 62            return res > 0 ? true : false;            
 63        }

 64ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 65        /// 执行带Sql参数的数据库增、删、改操作
 66        /// </summary>
 67        /// <param name="cmd">Sql命令</param>
 68        /// <param name="ct">Sql命令类型</param>
 69        /// <param name="paras">Sql参数</param>
 70        /// <returns>命令是否执行成功</returns>

 71        public bool ExcuteNonQuery(string cmd,CommandType ct,SqlParameter[] paras)
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 73            int res;
 74            this.Connect();
 75            sqlcmd.CommandText = cmd;
 76            sqlcmd.Connection = sqlconn;
 77            sqlcmd.CommandType = ct;
 78            sqlcmd.Parameters.Clear(); sqlcmd.Parameters.AddRange(paras);
 79            try
 80ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 81                res = sqlcmd.ExecuteNonQuery();
 82            }

 83            catch (Exception ex)
 84ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 85                throw (ex);
 86            }

 87            finally
 88ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 89                if (sqlconn.State == ConnectionState.Open)
 90                    sqlconn.Close();
 91            }

 92            return res > 0 ? true : false;        
 93        }

 94ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 95        /// 执行不带Sql参数的数据库查询命令
 96        /// </summary>
 97        /// <param name="cmd">Sql命令</param>
 98        /// <param name="ct">Sql命令类型</param>
 99        /// <returns>查询结果DataTable</returns>

100        public DataTable  ExcuteQuery(string cmd, CommandType ct)
101ExpandedSubBlockStart.gifContractedSubBlock.gif        {
102            DataTable dt=new DataTable();
103            this.Connect();
104            sqlcmd.CommandText = cmd;
105            sqlcmd.Connection = sqlconn;
106            sqlcmd.CommandType = ct;
107            try
108ExpandedSubBlockStart.gifContractedSubBlock.gif            {
109                sqldr = sqlcmd.ExecuteReader();
110                dt.Load(sqldr);
111            }

112            catch (Exception ex)
113ExpandedSubBlockStart.gifContractedSubBlock.gif            {
114                throw (ex);
115            }

116            finally
117ExpandedSubBlockStart.gifContractedSubBlock.gif            {
118                if (sqlconn.State == ConnectionState.Open)
119                    sqlconn.Close();
120            }

121            return dt;
122        }

123ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
124        /// 执行带Sql参数的数据库查询命令
125        /// </summary>
126        /// <param name="cmd">Sql命令</param>
127        /// <param name="ct">Sql命令类型</param>
128        /// <returns>查询结果DataTable</returns>

129        public DataTable ExcuteQuery(string cmd, CommandType ct,SqlParameter[] paras)
130ExpandedSubBlockStart.gifContractedSubBlock.gif        {
131            DataTable dt = new DataTable();
132            this.Connect();
133            sqlcmd.CommandText = cmd;
134            sqlcmd.Connection = sqlconn;
135            sqlcmd.CommandType = ct;
136            sqlcmd.Parameters.Clear(); sqlcmd.Parameters.AddRange(paras);
137            try
138ExpandedSubBlockStart.gifContractedSubBlock.gif            {
139                sqldr = sqlcmd.ExecuteReader();
140                dt.Load(sqldr);
141            }

142            catch (Exception ex)
143ExpandedSubBlockStart.gifContractedSubBlock.gif            {
144                throw (ex);
145            }

146            finally
147ExpandedSubBlockStart.gifContractedSubBlock.gif            {
148                if (sqlconn.State == ConnectionState.Open)
149                    sqlconn.Close();
150            }

151            return dt;
152        }

153    }

154}
 
155
156ExpandedBlockStart.gifContractedBlock.gif/**//*
157

158

NewsDAO,主要实现对新闻表的操作

ContractedBlock.gif ExpandedBlockStart.gif NewsDAO
 1*创建人:Cat_Lee
 2*创建时间:2009/8/20 22:03:56
 3*说明:对新闻进行数据库操作的类
 4*版权所有: Cat_Lee 
 5*欢迎访问我的Blog:http://www.cnblogs.com/cat-lee/
 6*/
 7using System;
 8using System.Collections.Generic;
 9using System.Linq;
10using System.Text;
11using System.Data;
12using System.Data.SqlClient;
13using Model; 
14
15namespace DAL
16ExpandedBlockStart.gifContractedBlock.gif{
17   public class NewsDAO
18ExpandedSubBlockStart.gifContractedSubBlock.gif    {
19       SQLHelper sqlHelper;
20       public NewsDAO()
21ExpandedSubBlockStart.gifContractedSubBlock.gif       {
22           sqlHelper = new SQLHelper();
23       }
      
24       //按CategoryID取出其下所有新闻,只取简要信息
25       public DataTable SelectbyCaID(int id)
26ExpandedSubBlockStart.gifContractedSubBlock.gif       {
27           string cmd = "SELECT News.ID,News.title,NewsCategory.name as CategoryName,News.publishTime,News.source,News.categoryID FROM News INNER JOIN NewsCategory ON News.categoryID = NewsCategory.ID where News.CategoryID=@id order by News.publishTime desc";
28ExpandedSubBlockStart.gifContractedSubBlock.gif           SqlParameter[] paras = new SqlParameter("@id", id) };
29           return sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
30       }

31       //选出所有新闻,由于取所有新闻时只需要标题等简要信息所以只取出简要信息。
32       public DataTable SelectAllNews()
33ExpandedSubBlockStart.gifContractedSubBlock.gif       {
34           string cmd = "SELECT News.ID,News.title,NewsCategory.name as CategoryName,News.publishTime,News.source,News.categoryID FROM News INNER JOIN NewsCategory ON News.categoryID = NewsCategory.ID order by News.publishTime desc";
35           return sqlHelper.ExcuteQuery(cmd, CommandType.Text);
36       }

37       //增加新闻
38       public bool Insert(News news)
39ExpandedSubBlockStart.gifContractedSubBlock.gif       {
40           string cmd = "InsertNewNews";
41ExpandedSubBlockStart.gifContractedSubBlock.gif           SqlParameter[] paras = new SqlParameter[]new SqlParameter("@author", news.Author),new SqlParameter("@categoryid",news.CaID),new SqlParameter("@content",news.Content),
42                                new SqlParameter("@ishot",news.IsHot),new SqlParameter("@isnew",news.IsNew),new SqlParameter("@isrecommend",news.IsRecommend),
43                                new SqlParameter ("@newsimages",news.NewsImages),new SqlParameter("@rank",news.Rank),new SqlParameter("@source",news.Source),
44                                new SqlParameter("@title",news.Title) , new SqlParameter ("@titleimages",news.TitleImages)}
;
45           return sqlHelper.ExcuteNonQuery(cmd, CommandType.StoredProcedure, paras);
46       }

47       //按ID删除新闻
48       public bool DeletebyID(int id)
49ExpandedSubBlockStart.gifContractedSubBlock.gif       {
50           string cmd = "delete news where id = @id";//本想全部都用存储过程写的,不过好像这里的确没有什么必要
51ExpandedSubBlockStart.gifContractedSubBlock.gif           SqlParameter[] paras = new SqlParameter("@id", id) };
52           return sqlHelper.ExcuteNonQuery(cmd, CommandType.Text,paras);
53       }

54       //按ID取出新闻,取出详细内容。
55       public News SelectbyID(int id)
56ExpandedSubBlockStart.gifContractedSubBlock.gif       {
57           string cmd = "select * from news where id = @id";
58ExpandedSubBlockStart.gifContractedSubBlock.gif           SqlParameter[] paras = new SqlParameter("@id", id) };
59           DataTable dt= sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
60           News n = new News();
61           n.ID = id; n.Author = dt.Rows[0]["author"].ToString(); n.CaID =(int) dt.Rows[0]["categoryID"];
62           n.Content = dt.Rows[0]["content"].ToString(); n.IsHot =(bool) dt.Rows[0]["ishot"]; n.IsNew = (bool)dt.Rows[0]["isNew"];
63           n.IsRecommend = (bool)dt.Rows[0]["isRecommend"]; n.NewsImages = dt.Rows[0]["newsImages"].ToString();
64           n.PublishTime = (DateTime) dt.Rows[0]["publishtime"]; n.Rank = (int)dt.Rows[0]["rank"]; n.Source = dt.Rows[0]["source"].ToString();
65           n.Title = dt.Rows[0]["title"].ToString(); n.TitleImages = dt.Rows[0]["titleImages"].ToString();
66           return n;
67       }

68       //按标题搜索新闻,取出简要信息
69       public DataTable SearchbyTitle(string key)
70ExpandedSubBlockStart.gifContractedSubBlock.gif       {
71           string cmd = "SELECT News.ID,News.title,NewsCategory.name,News.publishTime,News.source,News.categoryID FROM News INNER JOIN NewsCategory ON News.categoryID = NewsCategory.ID WHERE News.Title like '%'+@key+'%' order by News.publishTime desc";
72ExpandedSubBlockStart.gifContractedSubBlock.gif           SqlParameter[] paras = new SqlParameter("@key", key) };
73           return  sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
74       }

75        //按内容搜索新闻,取出简要信息
76       //按ID修改(更新)新闻
77       public bool Update(int id)
78ExpandedSubBlockStart.gifContractedSubBlock.gif       {
79           bool flag=false;
80           return flag;
81       }

82        //取出N条热点新闻,取出简要信息
83       public DataTable SelectHotNews()
84ExpandedSubBlockStart.gifContractedSubBlock.gif       {
85           return sqlHelper.ExcuteQuery("SelectNewNews", CommandType.StoredProcedure);
86       }

87       //取出N条最新新闻,取出简要信息
88       public DataTable SelectNewNews()
89ExpandedSubBlockStart.gifContractedSubBlock.gif       {
90           return sqlHelper.ExcuteQuery("SelectNewNews", CommandType.StoredProcedure);
91       }

92       //新闻排序
93    }

94}
 
95
96

 

ContractedBlock.gif ExpandedBlockStart.gif NewsCommentDAO
 1ExpandedBlockStart.gifContractedBlock.gif/**//*
 2*创建人:Cat_Lee
 3*创建时间:2009/10/8 11:02:31
 4*说明:新闻评论数据库操作类
 5*版权所有: Cat_Lee 
 6*欢迎访问我的Blog:http://www.cnblogs.com/cat-lee/
 7*/

 8using System;
 9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12using System.Data;
13using System.Data.SqlClient;
14using Model; 
15
16namespace DAL
17ExpandedBlockStart.gifContractedBlock.gif{
18    public class NewsCommentDAO
19ExpandedSubBlockStart.gifContractedSubBlock.gif    {
20        SQLHelper sqlHelper;
21       public NewsCommentDAO()
22ExpandedSubBlockStart.gifContractedSubBlock.gif       {
23           sqlHelper = new SQLHelper();
24       }
      
25        //按ID取出评论
26        public DataTable SelectCommentbyNewsID(int newsid)
27ExpandedSubBlockStart.gifContractedSubBlock.gif        {
28            string cmd = "select * from Newscomment where newsid=@newsid";
29ExpandedSubBlockStart.gifContractedSubBlock.gif            SqlParameter[] paras = new SqlParameter("@newsid", newsid) };
30            return sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
31        }

32        //添加评论
33        public bool AddComment(NewsComment ncom)
34ExpandedSubBlockStart.gifContractedSubBlock.gif        {
35            string cmd = "insert into newscomment(newsid,[user],[content]) values(@newsid,@user,@content)";
36ExpandedSubBlockStart.gifContractedSubBlock.gif            SqlParameter[] paras = new SqlParameter("@newsid", ncom.NewsID), new SqlParameter("@user", ncom.UserName), new SqlParameter("@content", ncom.Content) };
37            return sqlHelper.ExcuteNonQuery(cmd, CommandType.Text, paras);
38        }

39    }

40}
 
41
42
新闻类别数据库操作类
ContractedBlock.gif ExpandedBlockStart.gif Code
 1ExpandedBlockStart.gifContractedBlock.gif/**//*
 2*创建人:Cat_Lee
 3*创建时间:2009/10/8 10:55:23
 4*说明:新闻类别数据库操作类
 5*版权所有: Cat_Lee 
 6*欢迎访问我的Blog:http://www.cnblogs.com/cat-lee/
 7*/
 8using System;
 9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12using System.Data;
13using System.Data.SqlClient;
14using Model; 
15
16namespace DAL
17ExpandedBlockStart.gifContractedBlock.gif{
18    public class NewsCaDAO
19ExpandedSubBlockStart.gifContractedSubBlock.gif    {
20        SQLHelper sqlHelper;
21       public NewsCaDAO()
22ExpandedSubBlockStart.gifContractedSubBlock.gif       {
23           sqlHelper = new SQLHelper();
24       }

25       //取出所有类别
26       public DataTable SelectAllCategory()
27ExpandedSubBlockStart.gifContractedSubBlock.gif       {
28           string cmd = "select * from NewsCategory";
29           return sqlHelper.ExcuteQuery(cmd, CommandType.Text);
30       }

31       //按上级类别ID取出所有类别(取出某类别下所有子类别)
32       public DataTable SelectbyUpperCaID(int id)
33ExpandedSubBlockStart.gifContractedSubBlock.gif       {
34           string cmd;
35           if (id == -1)//取出顶层类别
36ExpandedSubBlockStart.gifContractedSubBlock.gif           {
37               cmd = "select * from NewsCategory where uppercaid is null";
38               return sqlHelper.ExcuteQuery(cmd, CommandType.Text);
39           }

40           else//取出子类别
41ExpandedSubBlockStart.gifContractedSubBlock.gif           {
42               cmd = "select * from NewsCategory where uppercaid=@id";
43ExpandedSubBlockStart.gifContractedSubBlock.gif               SqlParameter[] paras = new SqlParameter("@id", id) };
44               return sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
45           }

46       }

47    }

48}
 
49

小小菜鸟没有开发经验,对于本系列开发也没有做什么准备,所以其中不免有错误或遗漏,还请诸位不吝赐教,小弟在此感激不尽。另外,由于在做OnePiece的开发的同时我也在不断的学习和解决当中遇到的问题。所以文章发布的日期间隔或许会有些长,还请各位看官见谅。

下集预告:第六章 :

本站采用创作共用许可 署名,非商业 欢迎转载,转载请注明出处,并包括此段声明 。 Cat_Lee @ cnblogs

转载于:https://www.cnblogs.com/cat-lee/archive/2009/10/10/1580070.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值