Implement unlimited level classification method with C # and database

xiaoxiao2021-03-06  83

In doing software is, touch this problem, you have to repay it with a tree classification. Product classification should be in unlimited subclasses, how to design better. Use a database or use XML. Due to the previous design, programming is born, .NET also started learning, I feel that it is more familiar with the database. Due to the previous experience, it is more embarrassed to the database design. Citing a lot, such as a list, with a Parent (parent), Child (sub-link), later discarding Child, using a Parent, only the father, so the design database format is as follows: Table name: sort field: sortid (primary key, Self-growth), ParentID, SortName, Isend (whether it is the final class, because the final class can not be divided, the final class can only be specific. The opposite is not the final category, only Can set the category, not to set the specific product) The database is built, which is the simplest structure. Now explain the method of use of the library. The most important field of the SORT table is ParentID for link classification. For example, if you think about your father, you don't have to recognize the grandfather, Grandpa is the father's recognition, that is, you -> Father -> Grandpa. Only the respective father (PARENT), this chain is chained. Further, Grandpa can have a brother, but this is not what you consider, just appreciate the ancestors, not to take care of the branch (ie, the future generations of grandfather), is a thing of others. Again the database, the top class, ParentId = 0, explanation without parent class; if a class, its parent class is 2, then ParentID = 2; Wanli Long March, this start, want to use it has a lot of problems . For example, tree search, delete nodes, get each node information, etc. Here is I wrote in C #, with TreeView to indicate that the tree is clear, it is a big head, I don't know how to write, just provide a way of thinking. Have a good way to think about sharing. With the tree, I am very energetic, the tree is not good (just sorry to use .Net), such as you click on a child node (Node), to display the node information (including name or other information) . To illustrate this TreeView display tree, first you must know the TreeView to add a node method. The TreeView word point object is TREENODE, you can create TREENODE TN = New Treenode ("CustomerName"); // Add Node TreeView1.nodes [1] .Nodes [2] .add (TN); // If you want to add the word node TREENODE TN2 = New Treenode ("Name2"); Tn.Add (TN2); the above can only establish a fixed tree, the number of dynamic characters, and how much is uncertain If it is TN1, TN2 ..... I want to go to it.

So consider the collection of these NODEs in the array arraylist arrnode = new arraylist (); a simple add tree function (is a idea, in fact, it is not complete) NodeIndex: The position of the parent node in the array; NodeName: Node name Public void addnode (int nodeIndex, string nodename) {TREENODE TN = New Treenode (Nodename);

(TREENODE) ​​Arrnode [NodeIndex]). Add (tn);

// Add new Node to an array

Arrnode.Add (TN);} Hey, this is a function of the operator, seems to be achieved, in fact, yet. Because you read from the database will not inform you to add nodes under the array. Only the ID number and ParentID read from the database. How to implement it, it is really a hurt, because any ID and ParentID information are not seen in the array. This function is also greatly modified, where you can make each Node have ID and ParentID properties. I finally want to write my own TREENODE components. Next, you have to make TREENODEs. New components define a TREENODE derived class: public class exhenode: system.windows.forms.treenode and define a structure:

Public struct sort

{

Public int ID;

Public String Name;

Public int parentID;

Public Bool Isend;

Public bool disable;

} This structure is consistent with the database. The code of this class is relatively simple, as follows: use system; use system.ComponentModel; use system.drawing; using system.data; use system.windows.forms; namespace crystalbiz {

Public struct sort

{

Public int ID;

Public String Name;

Public int parentID;

Public Bool Isend;

Public bool disable;

}

///

/// ExNode extension

///

Public Class Exnode: System.Windows.Forms.treenode

{

PRIVATE SORT MYSORT;

Public Sort Sort

{

get

{

Return mysort;

}

set

{

Mysort = value;

THIS.TEXT = mysort.name;

}

}

Public exNode ()

{

mysort = new sort ();

}

}} The class defines the class, just change the original Treenode to Exnode. This addNode function can now be changed.

///

/// Add Node in TreeView and add Node to an array, easy to query

///

Private Void AddNode (Sort ST)

{

ExNode pnode = new exNode (); // To hook the father node

ExNode AddNode = New ExNode (); // This end

Addnode.sort = ST;

// Add to TreeView and array arrnode.add (nd);

IF (nd.sort.parentID == 0)

{

// The superior classification is added at the root node

Trvsort.nodes [0] .Nodes.Add (addnode);

}

Else

{

// Search for the parent class in the root directory, find it

Foreach (Exnode Node In Arrnode)

{

IF (node.sort.id == st.parentID)

{

PNODE = NODE;

Break;

}

}

/ / Add a new node under the parent node found

Pnode.nodes.add (addnode);

}

} The last step, the victory is looking at

///

/// Reset TreeView

/// resetSortView () function

///

#REGION ResetSortView () function implementation

Private void resetsortview ()

{

Trvsort.nodes.clear ();

Arrnode.clear ();

ExNode nd = new exNode ();

//

// Add product category

//

Sort mysort = new sort ();

mysort.id = 0;

mysort.name = "Classification of Commodities";

mysort.parentId = -1;

mysort.isnd = false;

mysort.disable = false;

Nd.sort = myster;

nd.imageIndex = 0;

nd.selectedImageIndex = 0;

Trvsort.Nodes.Add (Nd);

ArrNode.Add (ND);

// Open the database

// Sorry, I will define the database in Database, all in DBCLASS

// This is saved for SQL Server.

// DataSet here is also packaged for mydb.dbdataset

// Lazy to get the database, if you are not familiar with the database, learn quickly

String SQL = "Select * from merchandisesesort order by merchandises";

DBCLASS MYDB = New Dbclass ();

Mydb.dbopen ();

Mydb.createadapter (SQL);

Mydb.filldataSet ();

//

// Add data records one by one to the tree

//

For (int i = 1; i <= mydb.dbdatas.tables [0] .rows.count; i )

{

Mysort.id = (int) mydb.dbdataset.tables [0] .rows [i-1] ["merchandiseesortid"];

Mysort.name = mydb.dbdatas.tables [0] .rows [i-1] ["name"]. TOSTRING ();

MYSORT.PARENTID = (int) mydb.dbdatanet.tables [0] .rows [i-1] ["parentID"];

Mysort.isnd = (bool) mydb.dbdataset.tables [0] .rows [i-1] ["isend"];

Mysort.disable = (bool) mydb.dbdatanet.tables [0] .rows [i-1] ["disable"]; addnode (mysort);

}

Mydb.dbclose ();

Trvsort.expandall ();

} This code is definitely a problem. For example, in the end, it is possible to use DataReader more efficient, but I am thinking while learning, I don't think about these, and there are many other things, things have to be relatively important, solve big, then pursue performance. I prefer art. I used to pursue perfect thoughts, I didn't play here. Otherwise, for a piece of code, the result may not be able to do anything. The overall thinking of the design process is the most tight, overall, it is slightly poor, and it is not an excellent project because internal blocks can be continuously improved. In addition, I have seen some articles online, about XML, haven't come to study and carefully. If you are interested, everyone will discuss it. Have a friend suggest that search is recurable, but I have used arrays here, I don't know that better. Other methods of adding subclasses have not been written, it may be relatively simple. More complicated is to delete child nodes, such as deleting (abort) nodes, then the child nodes should be all deleted, which is to search all of the sub-trees. I am a weak item in this regard, so I use a method, that is, if there is a subclass, I can't delete it, and I am deleting the DOS. This is sure that the time will be strengthened later, but not now. I don't write the best procedure, I will only write better procedures. So don't ask for the highest efficiency. If you want to discuss this idea, come to me, but if you want to find a perfect thing, don't come to me. Supplement: In order to better implement the search and classification, introduce IDPATH (ID path concept) I found a simple method using the ID path, don't have to set the IDPATH field in the database, only modify one in Exnode, add a private Variable: private string midpath; // Classified ID composed of paths

Public string idpath;} set {midpath = value;}} and setting: // Add to TreeView and array arrnode.add (nd); if (nd.sort.parentID == 0) {TRVSORT.NODES [0] .NODES.ADD (ND); nd.idpath = "root // 0";} else {foreach (exhenode node in arrnode) {if (node.sort.id == st.parentID) {PNODE = Node; Break;}} pnode.nodes.add (address); addnode.idpath = pnode.idpath "//" nd.sort.parentid.tostring ()

转载请注明原文地址:https://www.9cbs.com/read-107151.html

New Post(0)