数据库:SqlServer2000
表:tree
表结构:
测试数据:
算法:使用递归实现
======================================
asp代码:












































































asp.net代码:
(请注意:asp.net1.1中用同一个Connection,在前一个DataReader没有Close情况下再创建DataReader会出现错误提示:已有打开的与此连接相关联的 DataReader,必须首先将它关闭
我的做法是每次都创建一个Connection,很浪费资源,不知道有没其他好办法.
)
1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Web;
7
using System.Web.SessionState;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.HtmlControls;
11
using System.Data.SqlClient;
12
13
namespace AspNetTest.Common
14
{
15
/// <summary>
16
/// tree 的摘要说明。
17
/// </summary>
18
19
public class tree : System.Web.UI.Page
20
{
21
private string tablename = "tree";
22
int current = 0;
23
int len = 0;
24
string[] arrPowerName;
25
string ConnectionString;
26
public tree()
27
{
28
ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
29
len = GetRecordCount();
30
arrPowerName = new string[len];
31
}
32
private void Page_Load(object sender, System.EventArgs e)
33
{
34
35
GetPowerName(0);
36
//CloseDbConnection();
37
//ResponseArray();
38
// 在此处放置用户代码以初始化页面
39
}
40
private void GetPowerName(int _ParentId)
41
{
42
int Id;
43
string PowerName;
44
string sql = "select Id,PowerName,Layer from " + tablename + " where ParentId=" + _ParentId;
45
try
46
{
47
SqlConnection conn = new SqlConnection(ConnectionString);
48
conn.Open();
49
SqlCommand cmd = new SqlCommand(sql, conn);
50
SqlDataReader dr = cmd.ExecuteReader();
51
while(dr.Read())
52
{
53
for(int j=0; j<int.Parse(dr["Layer"].ToString()); j++)
54
{
55
Response.Write(" ");
56
}
57
PowerName = dr["PowerName"].ToString();
58
Response.Write(PowerName + "<br>");
59
arrPowerName[current++] = PowerName;
60
Id = int.Parse(dr["Id"].ToString());
61
GetPowerName(Id);
62
}
63
dr.Close();
64
conn.Close();
65
}
66
catch(Exception ex)
67
{
68
Response.Write(ex.ToString());
69
}
70
}
71
private void ResponseArray()
72
{
73
int i;
74
for(i=0; i<len; i++)
75
{
76
Response.Write(arrPowerName[i] + "<br>");
77
}
78
}
79
private void CloseDbConnection()
80
{
81
//conn.Close();
82
}
83
private int GetRecordCount()
84
{
85
SqlConnection conn = new SqlConnection(ConnectionString);
86
string sql = "select count(id) from " + tablename;
87
conn.Open();
88
SqlCommand cmd = new SqlCommand(sql, conn);
89
int count = int.Parse(cmd.ExecuteScalar().ToString());
90
conn.Close();
91
return count;
92
}
93
94
Web 窗体设计器生成的代码
114
}
115
}
116

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

114

115

116

遍历结果:
代码下载:
asp代码:/Files/jiny-z/tree(asp).rar
asp.net代码:/Files/jiny-z/tree(asp.net).rar
*********************************************************
根据chill ,flower.b的提示,修改了代码。
原来:asp/asp.net代码中都是在数据集中循环,效率较低.。
现在:asp代码改为在数组中循环,asp.net代码改为在DataTable中循环。
*********************************************************
asp代码:










































































asp.net代码:




























































