您的位置:首页 > 数据库

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

2005-06-30 13:43 796 查看
这实际上是一个典型的树型结构的问题,每个节点都应该记录他父节点的信息,调用的时候用递归的方法调用就可以了。
首先我们声明一个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型
这一切多亏了快乐的帮助啊,我发觉他今天的帮我的那点时间真的让我明白了好多东西,让我知道怎么样去找出问题啊,我以前就是不知道怎么样去找出问题!
今天心里很开心的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐