You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
13 KiB

  1. using System;
  2. using System.Data;
  3. using System.Text;
  4. using System.Data.SqlClient;
  5. namespace Basic.DAL
  6. {
  7. /// <summary>
  8. /// ����
  9. /// </summary>
  10. public partial class news
  11. {
  12. /// <summary>
  13. /// ����һ������
  14. /// </summary>
  15. public int Add(Basic.Model.news model)
  16. {
  17. StringBuilder strSql = new StringBuilder();
  18. strSql.Append("insert into tb_news(");
  19. strSql.Append("class_id,parent_id,guid,title,url,path,seo_title,seo_keywords,seo_description,content,paixu,hits,status,user_id,add_time)");
  20. strSql.Append(" values (");
  21. strSql.Append("@class_id,@parent_id,@guid,@title,@url,@path,@seo_title,@seo_keywords,@seo_description,@content,@paixu,@hits,@status,@user_id,@add_time)");
  22. strSql.Append(";select @@IDENTITY");
  23. SqlParameter[] parameters = {
  24. new SqlParameter("@class_id", SqlDbType.Int,10),
  25. new SqlParameter("@parent_id", SqlDbType.Int,10),
  26. new SqlParameter("@guid", SqlDbType.NVarChar,50),
  27. new SqlParameter("@title", SqlDbType.NVarChar,100),
  28. new SqlParameter("@url", SqlDbType.NVarChar,255),
  29. new SqlParameter("@path", SqlDbType.NVarChar,255),
  30. new SqlParameter("@seo_title", SqlDbType.NVarChar,255),
  31. new SqlParameter("@seo_keywords", SqlDbType.NVarChar,255),
  32. new SqlParameter("@seo_description", SqlDbType.NVarChar,255),
  33. new SqlParameter("@content", SqlDbType.NText,1073741823),
  34. new SqlParameter("@paixu", SqlDbType.Int,10),
  35. new SqlParameter("@hits", SqlDbType.Int,10),
  36. new SqlParameter("@status", SqlDbType.Int,10),
  37. new SqlParameter("@user_id", SqlDbType.Int,10),
  38. new SqlParameter("@add_time", SqlDbType.DateTime,23)};
  39. parameters[0].Value = model.class_id;
  40. parameters[1].Value = model.parent_id;
  41. parameters[2].Value = model.guid;
  42. parameters[3].Value = model.title;
  43. parameters[4].Value = model.url;
  44. parameters[5].Value = model.path;
  45. parameters[6].Value = model.seo_title;
  46. parameters[7].Value = model.seo_keywords;
  47. parameters[8].Value = model.seo_description;
  48. parameters[9].Value = model.content;
  49. parameters[10].Value = model.paixu;
  50. parameters[11].Value = model.hits;
  51. parameters[12].Value = model.status;
  52. parameters[13].Value = model.user_id;
  53. parameters[14].Value = model.add_time;
  54. object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
  55. if (obj == null)
  56. {
  57. return 0;
  58. }
  59. else
  60. {
  61. return Convert.ToInt32(obj);
  62. }
  63. }
  64. /// <summary>
  65. /// ����һ������
  66. /// </summary>
  67. public bool Update(Basic.Model.news model)
  68. {
  69. StringBuilder strSql = new StringBuilder();
  70. strSql.Append("update tb_news set ");
  71. strSql.Append("class_id=@class_id,");
  72. strSql.Append("parent_id=@parent_id,");
  73. strSql.Append("guid=@guid,");
  74. strSql.Append("title=@title,");
  75. strSql.Append("url=@url,");
  76. strSql.Append("path=@path,");
  77. strSql.Append("seo_title=@seo_title,");
  78. strSql.Append("seo_keywords=@seo_keywords,");
  79. strSql.Append("seo_description=@seo_description,");
  80. strSql.Append("content=@content,");
  81. strSql.Append("paixu=@paixu,");
  82. strSql.Append("hits=@hits,");
  83. strSql.Append("status=@status,");
  84. strSql.Append("user_id=@user_id,");
  85. strSql.Append("add_time=@add_time");
  86. strSql.Append(" where id=@id");
  87. SqlParameter[] parameters = {
  88. new SqlParameter("@class_id", SqlDbType.Int,10),
  89. new SqlParameter("@parent_id", SqlDbType.Int,10),
  90. new SqlParameter("@guid", SqlDbType.NVarChar,50),
  91. new SqlParameter("@title", SqlDbType.NVarChar,100),
  92. new SqlParameter("@url", SqlDbType.NVarChar,255),
  93. new SqlParameter("@path", SqlDbType.NVarChar,255),
  94. new SqlParameter("@seo_title", SqlDbType.NVarChar,255),
  95. new SqlParameter("@seo_keywords", SqlDbType.NVarChar,255),
  96. new SqlParameter("@seo_description", SqlDbType.NVarChar,255),
  97. new SqlParameter("@content", SqlDbType.NText,1073741823),
  98. new SqlParameter("@paixu", SqlDbType.Int,10),
  99. new SqlParameter("@hits", SqlDbType.Int,10),
  100. new SqlParameter("@status", SqlDbType.Int,10),
  101. new SqlParameter("@user_id", SqlDbType.Int,10),
  102. new SqlParameter("@add_time", SqlDbType.DateTime,23),
  103. new SqlParameter("@id", SqlDbType.Int,4)};
  104. parameters[0].Value = model.class_id;
  105. parameters[1].Value = model.parent_id;
  106. parameters[2].Value = model.guid;
  107. parameters[3].Value = model.title;
  108. parameters[4].Value = model.url;
  109. parameters[5].Value = model.path;
  110. parameters[6].Value = model.seo_title;
  111. parameters[7].Value = model.seo_keywords;
  112. parameters[8].Value = model.seo_description;
  113. parameters[9].Value = model.content;
  114. parameters[10].Value = model.paixu;
  115. parameters[11].Value = model.hits;
  116. parameters[12].Value = model.status;
  117. parameters[13].Value = model.user_id;
  118. parameters[14].Value = model.add_time;
  119. parameters[15].Value = model.id;
  120. int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
  121. if (rows > 0)
  122. {
  123. return true;
  124. }
  125. else
  126. {
  127. return false;
  128. }
  129. }
  130. /// <summary>
  131. /// ɾ��һ������
  132. /// </summary>
  133. public bool Delete(int id)
  134. {
  135. StringBuilder strSql = new StringBuilder();
  136. strSql.Append("delete from tb_news");
  137. strSql.Append(" where id=@id");
  138. SqlParameter[] parameters = {
  139. new SqlParameter("@id", SqlDbType.Int,4)};
  140. parameters[0].Value = id;
  141. int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
  142. if (rows > 0)
  143. {
  144. return true;
  145. }
  146. else
  147. {
  148. return false;
  149. }
  150. }
  151. /// <summary>
  152. /// �õ�һ������ʵ��
  153. /// </summary>
  154. public Basic.Model.news GetModel(int id)
  155. {
  156. StringBuilder strSql = new StringBuilder();
  157. strSql.Append("select top 1 id,class_id,parent_id,guid,title,url,path,seo_title,seo_keywords,seo_description,content,paixu,hits,status,user_id,add_time from tb_news");
  158. strSql.Append(" where id=@id");
  159. SqlParameter[] parameters = {
  160. new SqlParameter("@id", SqlDbType.Int,4)};
  161. parameters[0].Value = id;
  162. Basic.Model.news model = new Basic.Model.news();
  163. DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
  164. if (ds.Tables[0].Rows.Count > 0)
  165. {
  166. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["id"].ToString()))
  167. {
  168. model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
  169. }
  170. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["class_id"].ToString()))
  171. {
  172. model.class_id = int.Parse(ds.Tables[0].Rows[0]["class_id"].ToString());
  173. }
  174. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["parent_id"].ToString()))
  175. {
  176. model.parent_id = int.Parse(ds.Tables[0].Rows[0]["parent_id"].ToString());
  177. }
  178. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["guid"].ToString()))
  179. {
  180. model.guid = ds.Tables[0].Rows[0]["guid"].ToString();
  181. }
  182. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["title"].ToString()))
  183. {
  184. model.title = ds.Tables[0].Rows[0]["title"].ToString();
  185. }
  186. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["url"].ToString()))
  187. {
  188. model.url = ds.Tables[0].Rows[0]["url"].ToString();
  189. }
  190. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["path"].ToString()))
  191. {
  192. model.path = ds.Tables[0].Rows[0]["path"].ToString();
  193. }
  194. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["seo_title"].ToString()))
  195. {
  196. model.seo_title = ds.Tables[0].Rows[0]["seo_title"].ToString();
  197. }
  198. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["seo_keywords"].ToString()))
  199. {
  200. model.seo_keywords = ds.Tables[0].Rows[0]["seo_keywords"].ToString();
  201. }
  202. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["seo_description"].ToString()))
  203. {
  204. model.seo_description = ds.Tables[0].Rows[0]["seo_description"].ToString();
  205. }
  206. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["content"].ToString()))
  207. {
  208. model.content = ds.Tables[0].Rows[0]["content"].ToString();
  209. }
  210. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["paixu"].ToString()))
  211. {
  212. model.paixu = int.Parse(ds.Tables[0].Rows[0]["paixu"].ToString());
  213. }
  214. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["hits"].ToString()))
  215. {
  216. model.hits = int.Parse(ds.Tables[0].Rows[0]["hits"].ToString());
  217. }
  218. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["status"].ToString()))
  219. {
  220. model.status = int.Parse(ds.Tables[0].Rows[0]["status"].ToString());
  221. }
  222. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["user_id"].ToString()))
  223. {
  224. model.user_id = int.Parse(ds.Tables[0].Rows[0]["user_id"].ToString());
  225. }
  226. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["add_time"].ToString()))
  227. {
  228. model.add_time = DateTime.Parse(ds.Tables[0].Rows[0]["add_time"].ToString());
  229. }
  230. return model;
  231. }
  232. else
  233. {
  234. return null;
  235. }
  236. }
  237. /// <summary>
  238. /// ���ò�ѯ��ҳ����
  239. /// </summary>
  240. public DataSet GetList(string _strWhere, string _orderby, int _pageSize, int _page, out int recordCount)
  241. {
  242. BasicPage bp = new BasicPage();
  243. StringBuilder str_Sql = new StringBuilder();
  244. str_Sql.Append("select top " + _pageSize + " * from tb_news");
  245. str_Sql.Append(" where ");
  246. str_Sql.Append(_strWhere);
  247. str_Sql.Append(" and id not in ");
  248. str_Sql.Append(" ( ");
  249. str_Sql.Append(" select top " + (_page - 1) * _pageSize + " id from tb_news ");
  250. str_Sql.Append(" where ");
  251. str_Sql.Append(_strWhere);
  252. str_Sql.Append(" order by ");
  253. str_Sql.Append(_orderby);
  254. str_Sql.Append(" ) ");
  255. str_Sql.Append(" order by ");
  256. str_Sql.Append(_orderby);
  257. DataSet dst = bp.SelectDataBase("tb_news", str_Sql.ToString());
  258. //
  259. recordCount = totlePage(showTotal(_strWhere), _pageSize);
  260. return dst;
  261. }
  262. //��ҳ��
  263. public int totlePage(int Total, int PageSize)
  264. {
  265. if (Total % PageSize == 0)
  266. {
  267. return Total / PageSize;
  268. }
  269. else
  270. {
  271. return Total / PageSize + 1;
  272. }
  273. }
  274. //������
  275. protected int showTotal(string strSql)
  276. {
  277. BasicPage bp = new BasicPage();
  278. int intTotal = 0;
  279. SqlDataReader myread = bp.getRead("select count(id) as CountId from tb_news where " + strSql);
  280. if (myread.Read())
  281. {
  282. intTotal = Convert.ToInt32(myread["CountId"].ToString());
  283. }
  284. myread.Close();
  285. return intTotal;
  286. }
  287. /// <summary>
  288. /// �޸�һ������
  289. /// </summary>
  290. public void UpdateField(int id, string strValue)
  291. {
  292. StringBuilder strSql = new StringBuilder();
  293. strSql.Append("update tb_news set " + strValue);
  294. strSql.Append(" where id=" + id);
  295. DbHelperSQL.ExecuteSql(strSql.ToString());
  296. }
  297. }
  298. }