一、数据库表
![](https://www.cnblogs.com/images/cnblogs_com/htht66/data.gif)
二、代码
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
示例代码
1
protected void Button1_Click(object sender, EventArgs e)
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
4
//会员编号
5
long userId = System.DateTime.Now.Ticks;
6
string introId = TextBox1.Text.Trim();
7
int level = 0;
8
int parentId = 0;
9
//此会员应该是介绍会员的下一级,level=paraent+1;
10
//用编号获取会员的主键和level
11
string strSql = "select * from users where userid="+introId;
12
SqlDataReader dr = SqlHelper.ExecuteReader(CommandType.Text,strSql,null);
13
if (dr.Read())
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
level = int.Parse(dr["LevelId"].ToString());
16
parentId = int.Parse(dr["pkid"].ToString());
17
}
18
dr.Close();
19
level = level + 1;
20
//添加
21
userDB.AddUser(userId, parentId, level);
22
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "AddUser", "alert('ok')", true);
23
}
24![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//********************************************************
26
* 获取会员所有下线
27
* 1.给一会员的编号
28
* 2.获取会员的id
29
* 3.找parentid为id的会员
30
*
31
* 递归调用
32
* ************************************************************/
33
void GetUsers()
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
35
int parentId=0;
36
string strSql = "select * from users where userid=666666";
37
SqlDataReader dr = SqlHelper.ExecuteReader(CommandType.Text, strSql, null);
38
if (dr.Read())
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
parentId = int.Parse(dr["pkid"].ToString());
41
}
42
dr.Close();
43![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
GetListByParentId(parentId);
45
}
46![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
private void GetListByParentId(int parentId)
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
49
string strSql2 = "select * from users where ParentId=" + parentId.ToString();
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
SqlDataReader dr2 = SqlHelper.ExecuteReader(CommandType.Text, strSql2, null);
52
53
while (dr2.Read())
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
ListBox1.Items.Add(dr2["userid"].ToString());
56
GetListByParentId(int.Parse(dr2["pkId"].ToString()));//递归调用
57
}
58
59
dr2.Close();
60
}
61
protected void Button2_Click(object sender, EventArgs e)
62![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
63
GetUsers();
64
}
人的一生应该这样度过:当他回首往事的时候,不会因为虚度年华而悔恨,也不会因为碌碌无为而羞愧。