码迷,mamicode.com
首页 > 数据库 > 详细

.net连接数据库递归

时间:2019-02-15 22:29:26      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:ali   HERE   json   load   return   连接数   nod   ==   nec   

private void Form1_Load(object sender, EventArgs e)
{
List<Regions> regions = GetRegions().Where(r => r.RegionPID == 0).ToList();
foreach (var item in regions)
{
TreeNode treeNode = new TreeNode();
treeNode.Text = item.RegionName;
treeNode.Tag = item.RegionID;
treeView1.Nodes.Add(treeNode);
GetTree(treeNode);
}
}
public void GetTree(TreeNode treeNode)
{
List<Regions> regions = GetRegions().Where(r => r.RegionPID == Convert.ToInt32(treeNode.Tag+"")).ToList();
foreach (var item in regions)
{
TreeNode tree = new TreeNode();
tree.Text = item.RegionName;
tree.Tag = item.RegionID;
treeNode.Nodes.Add(tree);
GetTree(tree);
}
}
public List<Regions> GetRegions()
{
using(SqlConnection sqlConnection=new SqlConnection("Password = sa; Persist Security Info=True;User ID = sa; Initial Catalog = DB0213; Data Source = 127.0.0.1"))
{
string sql = "select * from Regions";
using(SqlDataAdapter sqlDataAdapter=new SqlDataAdapter(sql, sqlConnection))
{
DataTable dt = new DataTable();
sqlDataAdapter.Fill(dt);
return JsonConvert.DeserializeObject<List<Regions>>(JsonConvert.SerializeObject(dt));
}
}
}

.net连接数据库递归

标签:ali   HERE   json   load   return   连接数   nod   ==   nec   

原文地址:https://www.cnblogs.com/LYYXXXX/p/10386140.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!