In the small project needs multi-level classification, try to learn to achieve multi-level classification. Because of the stored procedure itself is not very familiar with the use of the TreeView, so the recursive logic is implemented in C #, matching the number of databases to complete a multi-level classification of the acquisition method. The addition of the classification node should be said to be relatively simple, this article briefly.
database table: CategoryInfo
Field Name Type
CIID int//record serial number, self increment
Ciname nvarchar (20)//category name
Ciparent int//parent category Ordinal
Cilayer int//At the level
Cidescription nvarchar (200)//Description of classification
To get a stored procedure for a child classification
CREATE PROCEDURE [dbo]. [Category_getchild]
@cName nvarchar (20)
As
BEGIN
DECLARE @tmpID int
SELECT @tmpID =ciid from CategoryInfo
WHERE RTRIM (ciname) = RTRIM (@cName)
If (@tmpID is not NULL)
SELECT * from CategoryInfo
WHERE ciparent = @tmpID
ORDER BY Cilayer
End
To get a function of a child classification
Public ilist<categoryinfo> getchildcategories (ilist<categoryinfo> cinfos,string cName)
{
SqlConnection con = new SqlConnection (connectionString);
SqlCommand cmd = new SqlCommand ("Category_getchild", con);
Cmd.commandtype = CommandType.StoredProcedure;
Cmd. Parameters.Add (New SqlParameter (Param_cname, SqlDbType.NVarChar, 20));
Cmd. Parameters[param_cname]. Value = CName;
ilist<string> tmpnames = new list<string> (); Temporary storage-Acquired child
Try
{
Con. Open ();
SqlDataReader reader = cmd. ExecuteReader ();
if (reader. HasRows)
{
while (reader. Read ())
{
CategoryInfo cinfo = new CategoryInfo (
(int) reader["ciid"],
reader["Ciname"]. ToString (),
(int) reader["Ciparent"],
(int) reader["Cinum"],
reader["Cidescription"]. ToString (),
(int) reader["Cilayer"]
);
String tmpname = reader["Ciname"]. ToString ();
Cinfos.add (cinfo);//Add the sorted to Cinfos
Tmpnames.add (Tmpname),//Add the acquired subcategory name to Tmpnames
}
}
}
Catch
{
throw new ApplicationException ("Get classification Error!");
}
Finally
{
Con. Close ();
}
foreach (string C in Tmpnames)
{
Cinfos = Getchildcategories (cinfos,c); Recursive operations. Continue to get sub categories
}
return Cinfos;
}
Description: In this function, if Tmpnames is replaced by Ilist<categoryinfo>, that is, if the element that it adds is the same as Cinfos, if you want to remove one of the items, the Cinfos will remove the item at the same time. Because CategoryInfo is a class, it is a reference type, not a value type. So Tmpnames uses a string type to avoid this problem.
It's a bit cumbersome to call the above function directly, and the upper program also needs to create a Ilist<categoryinfo> object, so you can add a function to call the above function. This way, the upper program only needs to provide the category name and whether it contains two parameters for this level of classification.
Gets a subcategory in which the Boolean parameter indicates whether to include this level of classification
Public ilist<categoryinfo> getcategories (string cName, bool isincludeself)
{
ilist<categoryinfo> Cinfos = new list<categoryinfo> ();
Cinfos = Getchildcategories (Cinfos, cName);
if (isincludeself = = True)
{
Cinfos.insert (0, Getbyname (cName));//Get the classification according to the name, this is very simple, this article is slightly.
}
return Cinfos;
}
Note: When this is the case, the Web server acquires a subcategory with multiple round-trip between the database servers, which degrades performance. Using stored procedures to implement recursive logic, the way to directly return a subcategory list should have better performance, especially if the Web server is not on the same server as the database server, and is also affected by the network.