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 IEnumerableGetEveryOneByParent(int id) 13 { 14 DataTable dt=sqlHelper.GetDatatable("select * from ItemsNode where parentId =@id", 15 new SqlParameter("id",id)); 16 List list = new List (); 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 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 IEnumerableGetEveryOneByParent(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 DataTreeBLL; 5 using DataTreeModel; 6 7 namespace DataTree 8 { 9 public partial class Form1 : Form 10 { 11 public Form1() 12 { 13 InitializeComponent(); 14 } 15 16 private void Form1_Load(object sender, EventArgs e) 17 { 18 //获取所有的父节点对象 19 IEnumerableitems= new TreeNodeBLL().GetEveryOneByParent(0); 20 21 foreach(ItemsNode item in items) 22 { 23 //创建一个节点实例 24 TreeNode Fnode = new TreeNode(); 25 Fnode.Text = item.name; 26 Fnode.Tag = item; 27 treeView1.Nodes.Add(Fnode); 28 GetChildNode(Fnode,(int)item.Id); 29 } 30 treeView1.ExpandAll(); 31 } 32 //遍历子节点 33 protected void GetChildNode(TreeNode Pnode,int id) 34 { 35 //获取所有以此节点最为的父节点对象 36 IEnumerable items = new TreeNodeBLL().GetEveryOneByParent(id); 37 foreach (ItemsNode item in items) 38 { 39 TreeNode Fnode = new TreeNode(); 40 Fnode.Text = item.name; 41 Fnode.Tag = item;//把节点的Tag指向对象 42 Pnode.Nodes.Add(Fnode); 43 GetChildNode(Fnode, (int)item.Id); 44 } 45 } 46 47 private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) 48 { 49 ItemsNode item = (ItemsNode)e.Node.Tag; 50 tb_content.Text = item.content; 51 } 52 53 private void bt_modify_Click(object sender, EventArgs e) 54 { 55 ItemsNode item = (ItemsNode)treeView1.SelectedNode.Tag; 56 Frmodify form = new Frmodify(); 57 form.FName = item.name; 58 form.Content = item.content; 59 if (form.ShowDialog()== DialogResult.OK)//递交修改指令 60 { 61 item.name = form.FName; 62 item.content = form.Content; 63 new TreeNodeBLL().UpdateRecord(item); 64 treeView1.SelectedNode.Text = item.name; 65 tb_content.Text = item.content; 66 } 67 } 68 69 private void bt_Insert_1_Click(object sender, EventArgs e) 70 { 71 Frmodify form = new Frmodify(); 72 if (form.ShowDialog() == DialogResult.OK) 73 { 74 ItemsNode item = new ItemsNode(); 75 item.name=form.FName; 76 item.content=form.Content; 77 item.parentId = 0;//一级节点 78 new TreeNodeBLL().InsertRecord(item); 79 TreeNode Node = new TreeNode(); 80 Node.Text=item.name; 81 Node.Tag=item; 82 treeView1.Nodes.Add(Node); 83 } 84 } 85 86 private void bt_Insert2_Click(object sender, EventArgs e) 87 { 88 TreeNode CurrentNode = treeView1.SelectedNode; 89 Frmodify form = new Frmodify(); 90 if (form.ShowDialog() == DialogResult.OK) 91 { 92 ItemsNode item = (ItemsNode)CurrentNode.Tag; 93 item.name = form.FName; 94 item.content = form.Content; 95 item.parentId=item.Id; 96 new TreeNodeBLL().InsertRecord(item); 97 //显示到界面 98 TreeNode Node = new TreeNode(); 99 Node.Text = item.name; 100 Node.Tag = item; 101 CurrentNode.Nodes.Add(Node); 102 } 103 } 104 105 private void bt_Delete_Click(object sender, EventArgs e) 106 { 107 TreeNode Node = treeView1.SelectedNode; 108 Node.Remove(); 109 ItemsNode item = (ItemsNode)Node.Tag; 110 new TreeNodeBLL().DeleteAllNode((int)item.Id); 111 } 112 } 113 }
SQLhelper 类
1 using System.Data; 2 using System.Data.SqlClient; 3 using System.Configuration; 4 5 namespace DataTreeDAL 6 { 7 class sqlHelper 8 { 9 protected static readonly string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString; 10 //返回一个结果集 11 public static DataTable GetDatatable(string sql,params SqlParameter[] Paramers) 12 { 13 using (SqlConnection con = new SqlConnection(conStr))//创建一个连接 14 { 15 con.Open();//打开连接通道 16 using (SqlCommand cmd =con.CreateCommand())//创建一个命令对象 17 { 18 cmd.CommandText = sql; 19 cmd.Parameters.AddRange(Paramers); 20 SqlDataAdapter da = new SqlDataAdapter(cmd); 21 DataTable dt = new DataTable(); 22 da.Fill(dt); 23 return dt; 24 } 25 } 26 } 27 28 public static int ExecuteNonQuery(string sql, params SqlParameter[] Paramers) 29 { 30 using (SqlConnection con = new SqlConnection(conStr))//创建一个连接 31 { 32 con.Open();//打开连接通道 33 using (SqlCommand cmd = con.CreateCommand())//创建一个命令对象 34 { 35 cmd.CommandText = sql; 36 cmd.Parameters.AddRange(Paramers); 37 return cmd.ExecuteNonQuery(); 38 } 39 } 40 } 41 } 42 }