一类似传销的累加算法以及如何设计数据库!

这实际上是一个典型的树型结构的问题,每个节点都应该记录他父节点的信息,调用的时候用递归的方法调用就可以了。

首先我们声明一个DataTable型的类变量,以避免每次都要操作数据库,同时在页面上放三个Label。代码如下

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

namespace hualong
{
 /// <summary>
 /// test 的摘要说明。
 /// </summary>
 public class test : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Label Label3;
  private DataTable dt;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!IsPostBack)
   {
       string userName = this.User.Identity.Name.ToString();//在这里获得通过了验证的用户的名字,因为我设置的是Forms窗体验证
    string connString = "provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath(".")+"//hualong.mdb"+";User ID=admin;Jet OleDb:Database Password=";//在这里我的数据库用了密码
    string selString = string.Format("select Count(*) from clientele where parent_id='{0}'",userName);//这里找到的是登陆用户的直接下级节点的数目
    string selString1 = "select * from clientele";//这里把所有的数据取出来,一定不能够写where子句,否则显示所有节点数目的函数ShowAllCount会返回错误的数字,我在这里晕了好久,多亏了快乐的帮助
                OleDbConnection conn = new OleDbConnection(connString);
    OleDbCommand comm = new OleDbCommand(selString,conn);
    conn.Open();
    Label1.Text = ((int)comm.ExecuteScalar()).ToString();//这个Label显示的是当前登陆用户的直接下级节点数目
    Label3.Text = userName;//这个Label显示的是用户的名字
    conn.Close();
    OleDbDataAdapter adapter = new OleDbDataAdapter(selString1,conn);
    dt = new DataTable();
    adapter.Fill(dt);
    Label2.Text = ShowAllCount(userName).ToString();//这个Label显示的是当前登陆用户所有的节点的数目,他调用了函数ShowAllCount.

    dt.Dispose();//销毁dt,减轻服务器开销
   }
  }

//这个递归调用函数显示当前用户的所有下级节点
  private int ShowAllCount(string str)
  {
   DataRow[] drClass = dt.Select(string.Format("parent_id='{0}'",str));//把符合条件的记录找出来,负责递归将会陷入死循环
   int count = drClass.Length;
   if(count>0)
   {
    foreach( DataRow dr in drClass)
    {
     string newStr = dr["this_id"].ToString();
     count += ShowAllCount(newStr);
    }
   }
   return count;
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

数据库:

   id                parent_id       this_id

自动编号        string型          string型

这一切多亏了快乐的帮助啊,我发觉他今天的帮我的那点时间真的让我明白了好多东西,让我知道怎么样去找出问题啊,我以前就是不知道怎么样去找出问题!

今天心里很开心的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值