数据库结构
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;
}
}
}
}
}