YY部分仅供娱乐,请勿拍砖。正文部分,如有错误或不足,欢迎拍砖、指正。
第四章:DAL层设计及实现
呃,春哥保佑。最近俗事缠身,这个项目一直没有做得完,现在总算完成了个大概了。(呃,当然只是新闻模块)由于近期事情比较多,这个自己练手的项目就先把新闻模块做个模样出来,以后再继续了。
话说我们已经把数据库设计好了。系统的功能也基本有个大概的理解了,下面就开始编码吧。
DAL层主要实现对数据库的操作。写的时候有点偷懒,不好的地方大家指正。
SQLHelper类(由于代码本身有注释,这里就不多做解释了)
SQLHelper
1/**//*
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
17{
18 public class SQLHelper
19 {
20 private SqlConnection sqlconn;
21 private SqlDataReader sqldr;
22 private SqlCommand sqlcmd;
23 public SQLHelper()
24 {
25 sqlcmd = new SqlCommand();
26 sqlconn = new SqlConnection();
27 }
28 /**//// <summary>
29 /// 取得数据库连接
30 /// </summary>
31 private void Connect()
32 {
33 sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString();
34 sqlconn.Open();
35 }
36 /**//// <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)
43 {
44 int res;
45 this.Connect();
46 sqlcmd.CommandText = cmd;
47 sqlcmd.Connection = sqlconn;
48 sqlcmd.CommandType = ct;
49 try
50 {
51 res = sqlcmd.ExecuteNonQuery();
52 }
53 catch (Exception ex)
54 {
55 throw (ex);
56 }
57 finally
58 {
59 if (sqlconn.State == ConnectionState.Open)
60 sqlconn.Close();
61 }
62 return res > 0 ? true : false;
63 }
64 /**//// <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)
72 {
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
80 {
81 res = sqlcmd.ExecuteNonQuery();
82 }
83 catch (Exception ex)
84 {
85 throw (ex);
86 }
87 finally
88 {
89 if (sqlconn.State == ConnectionState.Open)
90 sqlconn.Close();
91 }
92 return res > 0 ? true : false;
93 }
94 /**//// <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)
101 {
102 DataTable dt=new DataTable();
103 this.Connect();
104 sqlcmd.CommandText = cmd;
105 sqlcmd.Connection = sqlconn;
106 sqlcmd.CommandType = ct;
107 try
108 {
109 sqldr = sqlcmd.ExecuteReader();
110 dt.Load(sqldr);
111 }
112 catch (Exception ex)
113 {
114 throw (ex);
115 }
116 finally
117 {
118 if (sqlconn.State == ConnectionState.Open)
119 sqlconn.Close();
120 }
121 return dt;
122 }
123 /**//// <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)
130 {
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
138 {
139 sqldr = sqlcmd.ExecuteReader();
140 dt.Load(sqldr);
141 }
142 catch (Exception ex)
143 {
144 throw (ex);
145 }
146 finally
147 {
148 if (sqlconn.State == ConnectionState.Open)
149 sqlconn.Close();
150 }
151 return dt;
152 }
153 }
154}
155
156/**//*
157
158
1/**//*
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
17{
18 public class SQLHelper
19 {
20 private SqlConnection sqlconn;
21 private SqlDataReader sqldr;
22 private SqlCommand sqlcmd;
23 public SQLHelper()
24 {
25 sqlcmd = new SqlCommand();
26 sqlconn = new SqlConnection();
27 }
28 /**//// <summary>
29 /// 取得数据库连接
30 /// </summary>
31 private void Connect()
32 {
33 sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString();
34 sqlconn.Open();
35 }
36 /**//// <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)
43 {
44 int res;
45 this.Connect();
46 sqlcmd.CommandText = cmd;
47 sqlcmd.Connection = sqlconn;
48 sqlcmd.CommandType = ct;
49 try
50 {
51 res = sqlcmd.ExecuteNonQuery();
52 }
53 catch (Exception ex)
54 {
55 throw (ex);
56 }
57 finally
58 {
59 if (sqlconn.State == ConnectionState.Open)
60 sqlconn.Close();
61 }
62 return res > 0 ? true : false;
63 }
64 /**//// <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)
72 {
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
80 {
81 res = sqlcmd.ExecuteNonQuery();
82 }
83 catch (Exception ex)
84 {
85 throw (ex);
86 }
87 finally
88 {
89 if (sqlconn.State == ConnectionState.Open)
90 sqlconn.Close();
91 }
92 return res > 0 ? true : false;
93 }
94 /**//// <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)
101 {
102 DataTable dt=new DataTable();
103 this.Connect();
104 sqlcmd.CommandText = cmd;
105 sqlcmd.Connection = sqlconn;
106 sqlcmd.CommandType = ct;
107 try
108 {
109 sqldr = sqlcmd.ExecuteReader();
110 dt.Load(sqldr);
111 }
112 catch (Exception ex)
113 {
114 throw (ex);
115 }
116 finally
117 {
118 if (sqlconn.State == ConnectionState.Open)
119 sqlconn.Close();
120 }
121 return dt;
122 }
123 /**//// <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)
130 {
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
138 {
139 sqldr = sqlcmd.ExecuteReader();
140 dt.Load(sqldr);
141 }
142 catch (Exception ex)
143 {
144 throw (ex);
145 }
146 finally
147 {
148 if (sqlconn.State == ConnectionState.Open)
149 sqlconn.Close();
150 }
151 return dt;
152 }
153 }
154}
155
156/**//*
157
158
NewsDAO,主要实现对新闻表的操作
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
16{
17 public class NewsDAO
18 {
19 SQLHelper sqlHelper;
20 public NewsDAO()
21 {
22 sqlHelper = new SQLHelper();
23 }
24 //按CategoryID取出其下所有新闻,只取简要信息
25 public DataTable SelectbyCaID(int id)
26 {
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";
28 SqlParameter[] paras = { new SqlParameter("@id", id) };
29 return sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
30 }
31 //选出所有新闻,由于取所有新闻时只需要标题等简要信息所以只取出简要信息。
32 public DataTable SelectAllNews()
33 {
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)
39 {
40 string cmd = "InsertNewNews";
41 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)
49 {
50 string cmd = "delete news where id = @id";//本想全部都用存储过程写的,不过好像这里的确没有什么必要
51 SqlParameter[] paras = { new SqlParameter("@id", id) };
52 return sqlHelper.ExcuteNonQuery(cmd, CommandType.Text,paras);
53 }
54 //按ID取出新闻,取出详细内容。
55 public News SelectbyID(int id)
56 {
57 string cmd = "select * from news where id = @id";
58 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)
70 {
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";
72 SqlParameter[] paras = { new SqlParameter("@key", key) };
73 return sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
74 }
75 //按内容搜索新闻,取出简要信息
76 //按ID修改(更新)新闻
77 public bool Update(int id)
78 {
79 bool flag=false;
80 return flag;
81 }
82 //取出N条热点新闻,取出简要信息
83 public DataTable SelectHotNews()
84 {
85 return sqlHelper.ExcuteQuery("SelectNewNews", CommandType.StoredProcedure);
86 }
87 //取出N条最新新闻,取出简要信息
88 public DataTable SelectNewNews()
89 {
90 return sqlHelper.ExcuteQuery("SelectNewNews", CommandType.StoredProcedure);
91 }
92 //新闻排序
93 }
94}
95
96
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
16{
17 public class NewsDAO
18 {
19 SQLHelper sqlHelper;
20 public NewsDAO()
21 {
22 sqlHelper = new SQLHelper();
23 }
24 //按CategoryID取出其下所有新闻,只取简要信息
25 public DataTable SelectbyCaID(int id)
26 {
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";
28 SqlParameter[] paras = { new SqlParameter("@id", id) };
29 return sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
30 }
31 //选出所有新闻,由于取所有新闻时只需要标题等简要信息所以只取出简要信息。
32 public DataTable SelectAllNews()
33 {
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)
39 {
40 string cmd = "InsertNewNews";
41 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)
49 {
50 string cmd = "delete news where id = @id";//本想全部都用存储过程写的,不过好像这里的确没有什么必要
51 SqlParameter[] paras = { new SqlParameter("@id", id) };
52 return sqlHelper.ExcuteNonQuery(cmd, CommandType.Text,paras);
53 }
54 //按ID取出新闻,取出详细内容。
55 public News SelectbyID(int id)
56 {
57 string cmd = "select * from news where id = @id";
58 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)
70 {
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";
72 SqlParameter[] paras = { new SqlParameter("@key", key) };
73 return sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
74 }
75 //按内容搜索新闻,取出简要信息
76 //按ID修改(更新)新闻
77 public bool Update(int id)
78 {
79 bool flag=false;
80 return flag;
81 }
82 //取出N条热点新闻,取出简要信息
83 public DataTable SelectHotNews()
84 {
85 return sqlHelper.ExcuteQuery("SelectNewNews", CommandType.StoredProcedure);
86 }
87 //取出N条最新新闻,取出简要信息
88 public DataTable SelectNewNews()
89 {
90 return sqlHelper.ExcuteQuery("SelectNewNews", CommandType.StoredProcedure);
91 }
92 //新闻排序
93 }
94}
95
96
NewsCommentDAO
1/**//*
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
17{
18 public class NewsCommentDAO
19 {
20 SQLHelper sqlHelper;
21 public NewsCommentDAO()
22 {
23 sqlHelper = new SQLHelper();
24 }
25 //按ID取出评论
26 public DataTable SelectCommentbyNewsID(int newsid)
27 {
28 string cmd = "select * from Newscomment where newsid=@newsid";
29 SqlParameter[] paras = { new SqlParameter("@newsid", newsid) };
30 return sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
31 }
32 //添加评论
33 public bool AddComment(NewsComment ncom)
34 {
35 string cmd = "insert into newscomment(newsid,[user],[content]) values(@newsid,@user,@content)";
36 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
新闻类别数据库操作类
1/**//*
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
17{
18 public class NewsCommentDAO
19 {
20 SQLHelper sqlHelper;
21 public NewsCommentDAO()
22 {
23 sqlHelper = new SQLHelper();
24 }
25 //按ID取出评论
26 public DataTable SelectCommentbyNewsID(int newsid)
27 {
28 string cmd = "select * from Newscomment where newsid=@newsid";
29 SqlParameter[] paras = { new SqlParameter("@newsid", newsid) };
30 return sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
31 }
32 //添加评论
33 public bool AddComment(NewsComment ncom)
34 {
35 string cmd = "insert into newscomment(newsid,[user],[content]) values(@newsid,@user,@content)";
36 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
Code
1/**//*
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
17{
18 public class NewsCaDAO
19 {
20 SQLHelper sqlHelper;
21 public NewsCaDAO()
22 {
23 sqlHelper = new SQLHelper();
24 }
25 //取出所有类别
26 public DataTable SelectAllCategory()
27 {
28 string cmd = "select * from NewsCategory";
29 return sqlHelper.ExcuteQuery(cmd, CommandType.Text);
30 }
31 //按上级类别ID取出所有类别(取出某类别下所有子类别)
32 public DataTable SelectbyUpperCaID(int id)
33 {
34 string cmd;
35 if (id == -1)//取出顶层类别
36 {
37 cmd = "select * from NewsCategory where uppercaid is null";
38 return sqlHelper.ExcuteQuery(cmd, CommandType.Text);
39 }
40 else//取出子类别
41 {
42 cmd = "select * from NewsCategory where uppercaid=@id";
43 SqlParameter[] paras = { new SqlParameter("@id", id) };
44 return sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
45 }
46 }
47 }
48}
49
1/**//*
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
17{
18 public class NewsCaDAO
19 {
20 SQLHelper sqlHelper;
21 public NewsCaDAO()
22 {
23 sqlHelper = new SQLHelper();
24 }
25 //取出所有类别
26 public DataTable SelectAllCategory()
27 {
28 string cmd = "select * from NewsCategory";
29 return sqlHelper.ExcuteQuery(cmd, CommandType.Text);
30 }
31 //按上级类别ID取出所有类别(取出某类别下所有子类别)
32 public DataTable SelectbyUpperCaID(int id)
33 {
34 string cmd;
35 if (id == -1)//取出顶层类别
36 {
37 cmd = "select * from NewsCategory where uppercaid is null";
38 return sqlHelper.ExcuteQuery(cmd, CommandType.Text);
39 }
40 else//取出子类别
41 {
42 cmd = "select * from NewsCategory where uppercaid=@id";
43 SqlParameter[] paras = { new SqlParameter("@id", id) };
44 return sqlHelper.ExcuteQuery(cmd, CommandType.Text, paras);
45 }
46 }
47 }
48}
49
小小菜鸟没有开发经验,对于本系列开发也没有做什么准备,所以其中不免有错误或遗漏,还请诸位不吝赐教,小弟在此感激不尽。另外,由于在做OnePiece的开发的同时我也在不断的学习和解决当中遇到的问题。所以文章发布的日期间隔或许会有些长,还请各位看官见谅。
下集预告:第六章 :