黑马程序员_递归查询

------- Windows Phone 7手机开发.Net培训、期待与您交流! -------

在开发程序中我们会经常用到递归,比如菜单的展示、TreeView控件节点数据的绑定等。

下面以查询slqserver数据库T_GoodsStock表中的数据,然后绑定到TreeView中为例。

数据库表T_GoodsStock:


代码如下:

窗体加载时,执行LoadGoodsToTree()方法,将数据加载绑定到TreeView控件上。

  1.   /// <summary>
  2.         /// 加载ParentId=-1的项到treeView结点上
  3.         /// </summary>
  4.         private void LoadGoodsToTree()
  5.         {
  6.             //加载前清空Nodes
  7.             treeViewGoods.Nodes.Clear();
  8.  
  9.             List<string> goodsList = new List<string>();
  10.             string sql = "select * from T_GoodsStock where ParentId=-1";
  11.             using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, CommandType.Text))
  12.             {
  13.                 if (reader.HasRows)
  14.                 {
  15.                     while (reader.Read())
  16.                     {
  17.                         GoodsStock gs = new GoodsStock();
  18.                         gs.Id = reader.GetInt32(0);
  19.                         gs.Goods = reader.GetString(1);
  20.                         TreeNode tNode = treeViewGoods.Nodes.Add(gs.ToString());
  21.                         tNode.Tag = gs.Id;
  22.                         //如果用户不是系统管理员,则没有添加、删除、修改类目功能
  23.                         if (UserMsg.level == 1)
  24.                         {
  25.                             tNode.ContextMenuStrip = ctmsType;
  26.                         }
  27.                         LoadSubToTree(tNode);
  28.                     }
  29.                 }
  30.             }
  31.         }

 递归加载节点的子数据

  1. /// <summary>
  2.         /// 递归加载到treeView上
  3.         /// </summary>
  4.         /// <param name="tNode"></param>
  5.         private void LoadSubToTree(TreeNode tNode)
  6.         {
  7.             string sql = "select * from T_GoodsStock where ParentId=@parentId";
  8.             SqlParameter pms = new SqlParameter("@parentId", tNode.Tag);
  9.             using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, CommandType.Text, pms))
  10.             {
  11.                 if (reader.HasRows)
  12.                 {
  13.                     while (reader.Read())
  14.                     {
  15.                         GoodsStock gs = new GoodsStock();
  16.                         gs.Id = reader.GetInt32(0);
  17.                         gs.Goods = reader.GetString(1);
  18.                         TreeNode tSubNode = tNode.Nodes.Add(gs.ToString());
  19.                         tSubNode.Tag = gs.Id;
  20.                         if (UserMsg.level == 1)
  21.                         {
  22.                             tSubNode.ContextMenuStrip = ctmsType;
  23.                         }
  24.                         LoadSubToTree(tSubNode);
  25.                     }
  26.                 }
  27.             }
  28.         }

 代码实现效果





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值