treeview(树加载)
数据库结构
id:int类型,主键,自增列;
Name:char类型;
paraid:int类型



窗台拖入控件treeview。
1.版本1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace _1.多级菜单
{
public partial class Form1 : Form
{
//获取链接
string ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnSring"].ToString();
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.LoadData(null);
}
/// <summary>
/// 载入tn的所有子节点,如果tn=null,说明是跟节点
/// </summary>
/// <param name="tn"></param>
private void LoadData(TreeNode tn)
{
if (tn == null)
{
//为空说明是根节点,添加节点时直接在控件的Nodes属性上添加就行了
using (SqlConnection conn = new SqlConnection(ConnString))
{
//查找根节点 所以parenti=0
string sql = "select * from Table_Tree where parentid=0";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
TreeNode cNode = new TreeNode();
cNode.Text = dr["name"].ToString();
cNode.Tag = dr["Id"];//tag没有意义,不被现实出来,所以就存储id
treeView1.Nodes.Add(cNode);//节点加到treeview
LoadData(cNode); }
}
}
} }
else
{
//参数tn!=null 那就是说要找tn的所有子节点,并做为tn的子节点.怎么在tn这个节点上添加子节点呢? tn.Nodes.Add(cNode);
using (SqlConnection conn = new SqlConnection(ConnString))
{
//查找根节点 所以parenti=0
string sql = "select * from Table_Tree where parentid=@id";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Id", tn.Tag);//tn是TreeNode对象
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{ while (dr.Read())
{
TreeNode cNode = new TreeNode();
cNode.Text = dr["name"].ToString();
cNode.Tag = dr["Id"];//tag没有意义,不被现实出来,所以就存储id
tn.Nodes.Add(cNode);
LoadData(cNode); }
}
}
} }
}
}
}
运行结果:
2.版本2(数据库相同)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace _02Tree
{
public partial class Form1 : Form
{
string ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString();
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.LoadData(null);
}
/// <summary>
/// 载入tn的所有子节点,如果tn=null,说明载的是根节点
/// </summary>
private void LoadData(TreeNode tn)
{
//获得要添加的节点
List<TblTree> addNodes = this.GetNodesByParentId(tn == null ? : Convert.ToInt32(tn.Tag)); foreach (var oneNode in addNodes)
{
TreeNode ctNode = new TreeNode();
ctNode.Text = oneNode.Name;
ctNode.Tag = oneNode.Id;
if (tn == null)
{
//如果是要节点,添加到控件上
treeView1.Nodes.Add(ctNode);
}
else
{
//如果tn不为null,说明要为tn添加子节点,所以我们就添加到tn上
tn.Nodes.Add(ctNode);
}
this.LoadData(ctNode);//调用自己,为ctNode添加子节点
}
}
private List<TblTree> GetNodesByParentId(int parentId)
{
List<TblTree> allNodes = new List<TblTree>(); string sql = "select * from Table_Tree where parentid=@id";
SqlDataReader dr = CommonCode.Sqlhelper.ExecuteReader(sql, new SqlParameter("@id", parentId));
while (dr.Read())
{
TblTree oneNode = new TblTree();
oneNode.Name = dr["name"].ToString();
oneNode.Id = Convert.ToInt32(dr["id"]);
oneNode.Parentid = parentId;
allNodes.Add(oneNode);
}
dr.Close();
return allNodes;
}
}
}
运行效果图:
还有TblTree类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace _02Tree
{
public class TblTree
{
int id; public int Id
{
get { return id; }
set { id = value; }
}
string name; public string Name
{
get { return name; }
set { name = value; }
}
int parentid; public int Parentid
{
get { return parentid; }
set { parentid = value; }
} }
}
数据库通用公共类:版本1和版本2通用
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text; namespace CommonCode
{
public static class Sqlhelper
{
private static readonly string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
//执行数据库无非就是执行cmd的以下方法
//1. ExecuteNonQuery 执行一个insert update delete
public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(parameters);
conn.Open();
return cmd.ExecuteNonQuery();
}
} }
//2.ExecuteScaler 返回结果集的第一行第一列
public static object ExecuteScaler(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(parameters);
conn.Open();
return cmd.ExecuteScalar();
}
}
} //3.ExecuteReader 返回一个Reader对象,用于读于多行多列数据
//在数据库上产生一个结果集,每次Read,从数据库服务器上读取一条数据回来,所以使用Reader读取数据,不能与数据库断开连接.
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] parameters)
{
SqlConnection conn = new SqlConnection(ConnectionString); using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(parameters);
conn.Open();
try
{
//在sqlhelper中,由于dr在返回给用户使用,所以注意dr不要使用using(){]
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); return dr; }
catch (Exception ex)
{
conn.Close();
throw ex; } }
} //4.执行一个sql语句,并把结果集放入本地的DataTable中.这里数据已
//经放到本地,断开数据库连接,还是可以访问到数据的.
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlDataAdapter da = new SqlDataAdapter(sql, conn))
{
da.SelectCommand.Parameters.AddRange(parameters);
DataTable dt = new DataTable();
//Fill方法其实执行的是da.SelectCommand中的sql语句,这里可以写conn.open也可以不写,如果不写,da会自动连接数据库
da.Fill(dt);
return dt;
}
}
}
}
}
treeview(树加载)的更多相关文章
- WinForm 进程、线程、TreeView递归加载、发送邮件--2016年12月13日
进程:一个程序就是一个进程,但是也有一个程序需要多个进程来支持的情况 进程要使用的类是:Process它在命名空间:System.Diagnostics; 静态方法Start(); Process.S ...
- winform进程、线程、TreeView递归加载
进程: 一般来说,一个程序就是一个进程,不过也有一个程序需要多个进程支持的情况. 进程所使用的类:Process 所需命名空间:System.Diagnostics; 可以通过进行来开启计算机上现有的 ...
- 页面全部加载完毕和页面dom树加载完毕
dom树加载完毕 $(document).ready()//原生写法document.ready = function (callback) { ///兼容FF,Google ...
- 伟景行 citymaker 从入门到精通(2)——工程图层树加载
工程树是指explorer左边这棵树 本例子实现了图层树加载,点击节点切换可视状态 树控件使用easyui的树 html部分 onCheck:treeProjectTreeOnCheck是指树节点的o ...
- Devexpress WinForm TreeList的三种数据绑定方式(DataSource绑定、AppendNode添加节点、VirtualTreeGetChildNodes(虚拟树加载模式))
第一种:DataSource绑定,这种绑定方式需要设置TreeList的ParentFieldName和KeyFieldName两个属性,这里需要注意的是KeyFieldName的值必须是唯一的. 代 ...
- Jquery插件 之 zTree树加载
原文链接:https://blog.csdn.net/jiaqu2177/article/details/80626730 zTree树加载 zTree 是一个依靠 jQuery 实现的多功能 “树插 ...
- asp.net treeview 异步加载
在使用TreeView控件的时候,如果数据量太大,这个TreeView控件加载会很慢,有时甚至加载失败, 为了更好的使用TreeView控件加载大量的数据,采用异步延迟加载TreeView. 在Tre ...
- 向treeview中加载数据
1.获取树节点的值,用事件AfterSelect加载(id值的获取,用name来获取) 2.双击treeview控件得到 private void treeView1_AfterSelect(obje ...
- 爱上MVC3~MVC+ZTree大数据异步树加载
回到目录 理论部分: MVC+ZTree:指在.net MVC环境下进行开发,ZTree是一个jquery的树插件 大数据:一般我们系统中,有一些表结构属于树型的,如分类,地域,菜单,网站导航等等,而 ...
- MVC小系列(十四)【MVC+ZTree大数据异步树加载】
ZTree是一个jquery的树插件可以异步加载 第一步定义一个标准的接口(指的是与ztree默认的数据元素保持一致) /// <summary> /// ZTree数据结构 /// &l ...
随机推荐
- 从零开始,制定PHP学习计划
7月份学习计划1-15 搭建开发环境.做个小demo 增删改查.Mysql数据库16-30号 架构设计.服务器管理.版本控制 8月份正式入手项目jquery脚本学习Thinksns开源学习.核心业务学 ...
- [转]Git学习笔记与IntelliJ IDEA整合
Git学习笔记与IntelliJ IDEA整合 一.Git学习笔记(基于Github) 1.安装和配置Git 下载地址:http://git-scm.com/downloads Git简要使用说明:h ...
- iOS10.0 & Swift 3.0 对于升级项目的建议
iOS & Swift新旧版本更替, 在Apple WWDC大会开始之际, 也迎来了iOS 10.0, Swift 3.0 测试版, 到目前为止, 已经是测试版2.0, 每次更新都带来了新的语 ...
- 勒布朗法则( LeBlanc)
看<代码整洁之道>看到了一个概念:勒布朗法则. 咦?这个不是NBA中的勒布朗·詹姆斯法则,当然NBA中针对一些球星的Bug表现也制定了一系列的法则,如乔丹法则(乔丹太过于强大).奥尼尔法则 ...
- mybatis由浅入深day02_7.3二级缓存
7.3 二级缓存 7.3.1 原理 下图是多个sqlSession请求UserMapper的二级缓存图解. 首先开启mybatis的二级缓存. sqlSession1去查询用户id为1的用户信息,查询 ...
- Mysql综合案例
Mysql综合案例 考核要点:创建数据表.单表查询.多表查询 已知,有一个学生表student和一个分数表score,请按要求对这两个表进行操作.student表和score分数表的表结构分别如表1- ...
- python中模块,包,库
模块:就是.py文件,里面定义了一些函数和变量,需要的时候就可以导入这些模块. 包:在模块之上的概念,为了方便管理而将文件进行打包.包目录下第一个文件便是 __init__.py,然后是一些模块文件和 ...
- Python 字符串处理(转)
转自:黄聪:Python 字符串操作(替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) http://www.cnblogs.com/huangcong/archive/2011/ ...
- 用rman恢复备库;遇到备库起不来一个案例 ORA-01152:ORA-01110
数据从主库恢复到备库:打开备库发现出现异常 SQL> alter database open; alter database open * ERROR at line 1: ORA-10458: ...
- Python cookielib 模块
什么是 cookie : 指某些网站为了辨别用户身份,进行 session 跟踪而储存在用户本地终端上的数据,通常以 txt 文件形式存储.比如你登录了淘宝,浏览器就会保存 cookie 信息,这样我 ...

