I started looking for unlimited categories of asp on the Internet, but I could hardly find asp.net, and the ones I found were combined with TreeView.
There are several codes for the asp version found, and it turns out that they all use recursive algorithms.
The table structures are as follows:
The table name is ClassName
id primary key
The id of the parent class corresponding to sid
ClassName corresponds to the name of the category.
Code snippet one:
1function loadNextType(upid,rank)
2 dimrs
3 set rs="select * from classname where sid="&upid
4 do while not rs.eof
5 loadNextType=loadNextType &rs("ClassName")&"<br>"& string("-",rank) & loadNextType(rs("id"),rank+1)
6rs.movenext
7 loop
When the 8end function is called: response.write(loadNextType(0,0))
The other piece of code is the same as the above principle. It just adds a tree structure display method.
Code snippet two:
1'Define the first level classification
2sub mainfl()
3 dimrs
4 set rs=conn.execute("select id,F_id,F_name from ClassNae where sid=0 order by id desc")
5 if not rs.eof then
6 do while not rs.eof
7 response.write rs(2) & "<br>"
8 call subfl(rs(0)," |-") 'Loop sub-level classification
9rs.movenext
10 if rs.eof then exit do 'To prevent an infinite loop
11 loops
12 end if
13end sub
14'Define sub-categories
15sub subfl(fid,strdis)
16 dimrs1
17 set rs1=conn.execute("select id,sid,ClassName from ClassName where sid="&fid&" order by id desc")
18 if not rs1.eof then
19 do while not rs1.eof
20 response.write rs1(2) & "<br>"
21 call subfl(rs1(0)," "&strdis) 'Recursive sub-level classification
22 rs1.movenext
23 if rs1.eof then
24 rs1.close
25 exit sub
26 end if
27 loop
28 end if
29end sub
I referred to the above code and changed it to the asp.net version of unlimited classification. I started to encounter grammatical limitations. The solution at that time was to directly replace rs with SqlDataReader and then modify it. The code is as follows (error code):
The table Tree structure of the test database is: id, ParentID, Name.
1private void Display(string parentid/**//*, int rank*/)
2 {
3 SqlDataReader dr;
4 SqlCommand cmd;
5 String strSQL;
6
7 strSQL = "Select * From Tree Where ParentID =" + parentid + "Order By ID DESC";
8 cmd = new SqlCommand(strSQL,conn);
9 //cmd.Connection.Open();
10
11 using(dr = cmd.ExecuteReader())
12 {
13 while(dr.Read())
14 {
15 Response.Write(dr["Name"].ToString() + "<br>");
16 Display(dr["ID"].ToString());
17}
18}
19 cmd.Connection.Close();
20}Call using Display("0").
The reason for the error is that SqlDataReader needs to be closed after each use, so DataReader cannot be nested.
Later, I changed to using DataTable to achieve unlimited classification, but I feel that this method is not efficient and needs improvement (still under research)
The modified code is as follows:
1private void Display(string parentid, String space)
2 {
3 DataTable dt;
4 String strSQL;
5 strSQL = "Select * From Tree Where ParentID =" + parentid + " Order By ID DESC";
6
7 SqlDataAdapter sda = new SqlDataAdapter(strSQL, conn);
8 DataSet ds = new DataSet();
9 sda.Fill(ds, "Tree");
10 dt = ds.Tables["Tree"];
11
12 if (dt.Rows.Count > 0)
13 {
14 foreach (DataRow dr in dt.Rows)
15 {
16 strOpinion += space + "<font color=red>[" + dr["Name"].ToString() +"<br>";
17 Display(dr["ID"].ToString(), " " + space, false);
18}
19}
20} Use Display("0","↓→→") when calling.
Although unlimited classification has been achieved, the efficiency is still quite low. We strive to explore higher efficiency.
Source: P.Dragon's Blog