一类似传销的累加算法以及如何设计数据库!
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型
这一切多亏了快乐的帮助啊,我发觉他今天的帮我的那点时间真的让我明白了好多东西,让我知道怎么样去找出问题啊,我以前就是不知道怎么样去找出问题!
今天心里很开心的
首先我们声明一个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型
这一切多亏了快乐的帮助啊,我发觉他今天的帮我的那点时间真的让我明白了好多东西,让我知道怎么样去找出问题啊,我以前就是不知道怎么样去找出问题!
今天心里很开心的
相关文章推荐
- 1.读写分离(主数据库写 可分拆副数据库读);2.垂直分库(将系统按业务进行了垂直拆分主数据库);3.水平分主库(ID生成算法决定了如何拆分数据,以及增删改查)
- 数据库之设计表:用户表信息以及第三方登录信息如何同步?
- [算法设计与分析]3.1.1循环设计要点(累加与累乘+判断完数+矩阵鞍点+打印规律图形)
- 【转】推荐:全面了解数据库设计中分类算法
- 公交车路线查询系统后台数据库设计--换乘算法改进与优化
- bloom filter(布隆过滤器)的算法设计与原理以及一些用例场景
- web聊天类数据库功能表结构如何设计?
- 用jsp设计网页的时候如何跟后台数据库相连
- 如何设计算法
- 如何设计数据库(一)
- 计算机网络,操作系统,数据结构,算法设计,计算机组成原理,数据库,概率论 零散知识库
- java 中的经典算法 如何将有序数据打散,以及在数组中取数据如何做到结果不重复。
- sqlite3常用命令以及django如何操作sqlite3数据库
- JAVA动态模块的设计实现以及如何通过newInstance()来创建带参的构造类实例
- awk 如何处理两个文件,实现类似数据库表连接的操作
- 如何设计折叠样式的屏幕(类似MIGO的屏幕)
- android第三方登录数据库设计以及业务数据流向说明
- 如何设计数据库表实现完整的RBAC(基于角色权限控制)
- 如何将文章带格式的存入数据库,并带格式的输出以及数据库经常插入空行问题
- 如果系统要使用超大整数(超过long长度范围),请你设计一个数据结构来存储这种超大型数字以及设计一种算法来实现超大整数加法运算