掌握TreeView用法
DAL层代码:
1 using System.Collections.Generic;
2 using System.Data;
3 using DataTreeModel;
4 using System.Data.SqlClient;
5 using System.Windows.Forms;
6
7 namespace DataTreeDAL
8 {
9 class TreesNodeDAL
10 {
11 //根据Parentid返回表中所有的的实体对象
12 public IEnumerable<ItemsNode> GetEveryOneByParent(int id)
13 {
14 DataTable dt=sqlHelper.GetDatatable("select * from ItemsNode where parentId =@id",
15 new SqlParameter("id",id));
16 List<ItemsNode> list = new List<ItemsNode>();
17 foreach (DataRow row in dt.Rows)
18 {
19 list.Add(CreateItemsNose(row));//添加对象
20 }
21 return list;
22 }
23 //更新表中的记录
24 public int UpdateRecord(ItemsNode item)
25 {
26 return sqlHelper.ExecuteNonQuery("update ItemsNode set name=@name,content=@content where id=@id", new SqlParameter("name", item.name), new SqlParameter("content", item.content),new
27 SqlParameter("id",item.Id));
28 }
29
30 public int InsertRecord(ItemsNode item)
31 {
32 return sqlHelper.ExecuteNonQuery("Insert into ItemsNode (name,content,parentId) values(@name,@content,@parentId)", new SqlParameter("name", item.name), new SqlParameter("content", item.content), new
33 SqlParameter("parentId", item.parentId));
34 }
35
36 public void DeleteNode(int ParentId)
37 {
38 //获得以该节点为父节点的所有子节点
39 IEnumerable<ItemsNode> childens = GetEveryOneByParent(ParentId);
40 foreach (ItemsNode child in childens)
41 {
42 DeleteNode((int)child.Id);
43 }
44 Delete(ParentId);
45 }
46
47 //实例化对象
48 private static ItemsNode CreateItemsNose(DataRow row)
49 {
50 ItemsNode item = new ItemsNode();
51 item.Id = row.IsNull("Id")?null:(int?)row["Id"];
52 item.name = (string)row["name"];
53 item.parentId = row.IsNull("parentId") ? null : (int?)row["parentId"];
54 item.content = (string)row["content"];
55 return item;
56 }
57 //删除操作
58 public int Delete(int id)
59 {
60 return sqlHelper.ExecuteNonQuery("delete from ItemsNode where id=@id",
61 new SqlParameter("id", id));
62 }
63 }
64 }
BLL层代码:
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using DataTreeDAL;
5 using DataTreeModel;
6
7 namespace DataTreeBLL
8 {
9 class TreeNodeBLL
10 {
11 //获得所有对象
12 public IEnumerable<ItemsNode> GetEveryOneByParent(int id)
13 {
14 return new TreesNodeDAL().GetEveryOneByParent(id);
15 }
16 //更新
17 public int UpdateRecord(ItemsNode item)
18 {
19 return new TreesNodeDAL().UpdateRecord( item);
20 }
21
22 //增加
23 public int InsertRecord(ItemsNode item)
24 {
25 return new TreesNodeDAL().InsertRecord(item);
26 }
27
28 public void DeleteAllNode(int ParentId)
29 {
30 new TreesNodeDAL().DeleteNode(ParentId);
31 }
32 }
33 }
UI层代码:
1 using System;
2 using System.Collections.Generic;
3 using System.Windows.Forms;
4 using
补充:Web开发 , ASP.Net ,