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.

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