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.

282 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. /// �����б�
  9. /// </summary>
  10. public partial class attribute
  11. {
  12. /// <summary>
  13. /// ����һ������
  14. /// </summary>
  15. public int Add(Basic.Model.attribute model)
  16. {
  17. StringBuilder strSql = new StringBuilder();
  18. strSql.Append("insert into tb_attribute(");
  19. strSql.Append("class_id,title,guid,parent_id,class_list,class_layer,paixu,url,path,content)");
  20. strSql.Append(" values (");
  21. strSql.Append("@class_id,@title,@guid,@parent_id,@class_list,@class_layer,@paixu,@url,@path,@content)");
  22. strSql.Append(";select @@IDENTITY");
  23. SqlParameter[] parameters = {
  24. new SqlParameter("@class_id", SqlDbType.Int,10),
  25. new SqlParameter("@title", SqlDbType.NVarChar,100),
  26. new SqlParameter("@guid", SqlDbType.NVarChar,50),
  27. new SqlParameter("@parent_id", SqlDbType.Int,10),
  28. new SqlParameter("@class_list", SqlDbType.NVarChar,500),
  29. new SqlParameter("@class_layer", SqlDbType.Int,10),
  30. new SqlParameter("@paixu", SqlDbType.Int,10),
  31. new SqlParameter("@url", SqlDbType.NVarChar,255),
  32. new SqlParameter("@path", SqlDbType.NVarChar,255),
  33. new SqlParameter("@content", SqlDbType.NVarChar,500)};
  34. parameters[0].Value = model.class_id;
  35. parameters[1].Value = model.title;
  36. parameters[2].Value = model.guid;
  37. parameters[3].Value = model.parent_id;
  38. parameters[4].Value = model.class_list;
  39. parameters[5].Value = model.class_layer;
  40. parameters[6].Value = model.paixu;
  41. parameters[7].Value = model.url;
  42. parameters[8].Value = model.path;
  43. parameters[9].Value = model.content;
  44. object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
  45. if (obj == null)
  46. {
  47. return 0;
  48. }
  49. else
  50. {
  51. return Convert.ToInt32(obj);
  52. }
  53. }
  54. /// <summary>
  55. /// ����һ������
  56. /// </summary>
  57. public bool Update(Basic.Model.attribute model)
  58. {
  59. StringBuilder strSql = new StringBuilder();
  60. strSql.Append("update tb_attribute set ");
  61. strSql.Append("class_id=@class_id,");
  62. strSql.Append("title=@title,");
  63. strSql.Append("guid=@guid,");
  64. strSql.Append("parent_id=@parent_id,");
  65. strSql.Append("class_list=@class_list,");
  66. strSql.Append("class_layer=@class_layer,");
  67. strSql.Append("paixu=@paixu,");
  68. strSql.Append("url=@url,");
  69. strSql.Append("path=@path,");
  70. strSql.Append("content=@content");
  71. strSql.Append(" where id=@id");
  72. SqlParameter[] parameters = {
  73. new SqlParameter("@class_id", SqlDbType.Int,10),
  74. new SqlParameter("@title", SqlDbType.NVarChar,100),
  75. new SqlParameter("@guid", SqlDbType.NVarChar,50),
  76. new SqlParameter("@parent_id", SqlDbType.Int,10),
  77. new SqlParameter("@class_list", SqlDbType.NVarChar,500),
  78. new SqlParameter("@class_layer", SqlDbType.Int,10),
  79. new SqlParameter("@paixu", SqlDbType.Int,10),
  80. new SqlParameter("@url", SqlDbType.NVarChar,255),
  81. new SqlParameter("@path", SqlDbType.NVarChar,255),
  82. new SqlParameter("@content", SqlDbType.NVarChar,500),
  83. new SqlParameter("@id", SqlDbType.Int,4)};
  84. parameters[0].Value = model.class_id;
  85. parameters[1].Value = model.title;
  86. parameters[2].Value = model.guid;
  87. parameters[3].Value = model.parent_id;
  88. parameters[4].Value = model.class_list;
  89. parameters[5].Value = model.class_layer;
  90. parameters[6].Value = model.paixu;
  91. parameters[7].Value = model.url;
  92. parameters[8].Value = model.path;
  93. parameters[9].Value = model.content;
  94. parameters[10].Value = model.id;
  95. int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
  96. if (rows > 0)
  97. {
  98. return true;
  99. }
  100. else
  101. {
  102. return false;
  103. }
  104. }
  105. /// <summary>
  106. /// ɾ��һ������
  107. /// </summary>
  108. public bool Delete(int id)
  109. {
  110. StringBuilder strSql = new StringBuilder();
  111. strSql.Append("delete from tb_attribute");
  112. strSql.Append(" where id=@id");
  113. SqlParameter[] parameters = {
  114. new SqlParameter("@id", SqlDbType.Int,4)};
  115. parameters[0].Value = id;
  116. int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
  117. if (rows > 0)
  118. {
  119. return true;
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. /// <summary>
  127. /// �õ�һ������ʵ��
  128. /// </summary>
  129. public Basic.Model.attribute GetModel(int id)
  130. {
  131. StringBuilder strSql = new StringBuilder();
  132. strSql.Append("select top 1 id,class_id,title,guid,parent_id,class_list,class_layer,paixu,url,path,content from tb_attribute");
  133. strSql.Append(" where id=@id");
  134. SqlParameter[] parameters = {
  135. new SqlParameter("@id", SqlDbType.Int,4)};
  136. parameters[0].Value = id;
  137. Basic.Model.attribute model = new Basic.Model.attribute();
  138. DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
  139. if (ds.Tables[0].Rows.Count > 0)
  140. {
  141. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["id"].ToString()))
  142. {
  143. model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
  144. }
  145. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["class_id"].ToString()))
  146. {
  147. model.class_id = int.Parse(ds.Tables[0].Rows[0]["class_id"].ToString());
  148. }
  149. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["title"].ToString()))
  150. {
  151. model.title = ds.Tables[0].Rows[0]["title"].ToString();
  152. }
  153. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["guid"].ToString()))
  154. {
  155. model.guid = ds.Tables[0].Rows[0]["guid"].ToString();
  156. }
  157. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["parent_id"].ToString()))
  158. {
  159. model.parent_id = int.Parse(ds.Tables[0].Rows[0]["parent_id"].ToString());
  160. }
  161. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["class_list"].ToString()))
  162. {
  163. model.class_list = ds.Tables[0].Rows[0]["class_list"].ToString();
  164. }
  165. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["class_layer"].ToString()))
  166. {
  167. model.class_layer = int.Parse(ds.Tables[0].Rows[0]["class_layer"].ToString());
  168. }
  169. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["paixu"].ToString()))
  170. {
  171. model.paixu = int.Parse(ds.Tables[0].Rows[0]["paixu"].ToString());
  172. }
  173. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["url"].ToString()))
  174. {
  175. model.url = ds.Tables[0].Rows[0]["url"].ToString();
  176. }
  177. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["path"].ToString()))
  178. {
  179. model.path = ds.Tables[0].Rows[0]["path"].ToString();
  180. }
  181. if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["content"].ToString()))
  182. {
  183. model.content = ds.Tables[0].Rows[0]["content"].ToString();
  184. }
  185. return model;
  186. }
  187. else
  188. {
  189. return null;
  190. }
  191. }
  192. /// <summary>
  193. /// ȡ�����������б�
  194. /// </summary>
  195. /// <param name="parent_id">��ID</param>
  196. /// <param name="class_id">����ID</param>
  197. /// <returns></returns>
  198. public DataTable GetList(int parent_id, int class_id)
  199. {
  200. StringBuilder strSql = new StringBuilder();
  201. strSql.Append("select * from tb_attribute");
  202. strSql.Append(" where class_id=" + class_id + " order by paixu desc,id asc");
  203. DataSet ds = DbHelperSQL.Query(strSql.ToString());
  204. DataTable oldData = ds.Tables[0] as DataTable;
  205. if (oldData == null)
  206. {
  207. return null;
  208. }
  209. //���ƽṹ
  210. DataTable newData = oldData.Clone();
  211. //���õ������ϳ�DAGATABLE
  212. GetChilds(oldData, newData, parent_id, class_id);
  213. return newData;
  214. }
  215. /// <summary>
  216. /// ���ڴ���ȡ�������¼������б�������������
  217. /// </summary>
  218. private void GetChilds(DataTable oldData, DataTable newData, int parent_id, int pro_class_id)
  219. {
  220. DataRow[] dr = oldData.Select("parent_id=" + parent_id);
  221. for (int i = 0; i < dr.Length; i++)
  222. {
  223. //����һ������
  224. DataRow row = newData.NewRow();
  225. row["id"] = int.Parse(dr[i]["id"].ToString());
  226. row["class_id"] = int.Parse(dr[i]["class_id"].ToString());
  227. row["title"] = dr[i]["title"].ToString();
  228. row["guid"] = dr[i]["guid"].ToString();
  229. row["parent_id"] = int.Parse(dr[i]["parent_id"].ToString());
  230. row["class_list"] = dr[i]["class_list"].ToString();
  231. row["class_layer"] = int.Parse(dr[i]["class_layer"].ToString());
  232. row["paixu"] = int.Parse(dr[i]["paixu"].ToString());
  233. row["url"] = dr[i]["url"].ToString();
  234. row["path"] = dr[i]["path"].ToString();
  235. row["content"] = dr[i]["content"].ToString();
  236. newData.Rows.Add(row);
  237. //������������
  238. this.GetChilds(oldData, newData, int.Parse(dr[i]["id"].ToString()), pro_class_id);
  239. }
  240. }
  241. /// <summary>
  242. /// �޸�һ������
  243. /// </summary>
  244. public void UpdateField(int id, string strValue)
  245. {
  246. StringBuilder strSql = new StringBuilder();
  247. strSql.Append("update tb_attribute set " + strValue);
  248. strSql.Append(" where id=" + id);
  249. DbHelperSQL.ExecuteSql(strSql.ToString());
  250. }
  251. /// <summary>
  252. /// ����ǰ��������
  253. /// </summary>
  254. public DataSet GetList(int Top, string strWhere, string filedOrder)
  255. {
  256. StringBuilder strSql = new StringBuilder();
  257. strSql.Append("select ");
  258. if (Top > 0)
  259. {
  260. strSql.Append(" top " + Top.ToString());
  261. }
  262. strSql.Append("id,class_id,title,guid,parent_id,class_list,class_layer,paixu,url,path,content");
  263. strSql.Append(" from tb_attribute");
  264. if (strWhere.Trim() != "")
  265. {
  266. strSql.Append(" where " + strWhere);
  267. }
  268. strSql.Append(" order by " + filedOrder);
  269. return DbHelperSQL.Query(strSql.ToString());
  270. }
  271. }
  272. }