Searches for all child and parent levels of a node in an unlimited category.

Source: Internet
Author: User

We have been trying to implement this function. Now we can see someone has made it. Let's take a look.

 

Unlimited data table:

SQL Code :
Create Table t_power (
ID Int Not Null Identity ( 1 , 1 ) Primary key, -- Permission ID
Parentid Int Not Null   Default ( 0 ), -- Parent ID
Depath Int Not Null   Default ( 0 ), -- Level
[Name] varchar ( 100 ) Default ( '' ) Not Null , -- Name
Ismenu Int Not Null   Default ( 0 ), --- Is it a menu item?
)
Go
Find all sublevels of a node:

C # code:
///  
/// Get sub-level return Sub-Level
///  
/// Parent ID
/// Sublevel list
Public   Static   Void Getchild ( Int Parentid, Ref Ilist powerlist)
{
String Strsql =   " Select * From t_power where parentid = "   + Parentid. tostring ();
Using (Sqldatareader Dr = SQL. executereader (strsql ))
{
If ( ! Dr. hasrows)
Return ;
While (Dr. Read ())
{
Pinfop =   New Pinfo ();
P. ID = Convert. toint32 (Dr [ " ID " ]);
P. parentid = Convert. toint32 (Dr [ " Parentid " ]);
P. depath = Convert. toint32 (Dr [ " Depath " ]);
P. Name = Dr [ " Name " ]. Tostring ();
P. ismenu = Convert. toint32 (Dr [ " Ismenu " ]);
Powerlist. Add (P );
Getchild (convert. toint32 (Dr [ " ID " ]), Ref Powerlist );
}
}
} Search for all parent levels:

C # code:
///  
/// Retrieve all ancestors
///  
/// Current ID
/// List
Public   Static Ilist getparentpower ( Int Powerid ){
Ilist powerlist =   New List ();
String Strsql =   @"
Declare @ temptable table
(
[ID] int,
Parentid int,
Depath int,
[Name] varchar (100 ),
Ismenu int,
URL varchar (200)
)

Declare @ oldid int
Declare @ ID int
Set @ ID = {0}
Set @ oldid = @ ID
While (@ ID> 0)
Begin
Select @ ID = parentid from t_power where id = @ ID;
If @ oldid = @ ID
Break
If (@ ID> 0)
Begin
Insert into @ temptable select * From t_power where id = @ ID;
End
End
Select * From @ temptable order by ID ASC
" ;
Strsql =   String . Format (strsql, powerid );
Using (Sqldatareader Dr = SQL. executereader (strsql ))
{
While (Dr. Read ())
{
Powerinfo P =   New Powerinfo ();
P. ID = Convert. toint32 (Dr [ " ID " ]);
P. parentid = Convert. toint32 (Dr [ " Parentid " ]);
P. depath = Convert. toint32 (Dr [ " Depath " ]);
P. Name = Dr [ " Name " ]. Tostring ();
P. ismenu = Convert. toint32 (Dr [ " Ismenu " ]);
Powerlist. Add (P );
}
}
Return Powerlist;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.