| |

VerySource

 Forgot password?
 Register
Search
View: 779|Reply: 8

When deleting the parent node of the treeview, how to delete all the directories corresponding to the child nodes in the

[Copy link]

4

Threads

12

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-2-20 16:00:01
| Show all posts |Read mode
My table structure is as follows, "id" is the primary key (this is one of my tree directories)

id parentid name
51 -1 r
52 -1 a
53 52 b
54 52 c
55 53 d
56 53 e
57 54 f
58 55 g

My purpose is to use this table to generate a treeview, delete the parent node id 52 in the tree directory, and in the database, record the id 52 and the corresponding database records of all child nodes under the parent node 52 Also delete.
Please give the code, the database will not be processed in this place.
Someone told me that you can use a recursion to write out all the ID children of this ID after you give it an ID. How do I get all the children of this ID?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-1 09:15:02
| Show all posts
Is a recursive plug, if it is the actual path, you can use the database to match the string plug
Reply

Use magic Report

1

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-5-3 23:15:01
| Show all posts
You only need to search the database based on the id of the parent node. You write a recursive pass in an id and then find where parentid = id. You can return a datatable. Then use foreach (dr in datatable.rows) to determine whether it has child nodes. Then in this foreach, you can declare a datatable and call this recursive method according to the id of dr. No, it's fine.
Reply

Use magic Report

1

Threads

26

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-5-4 17:30:01
| Show all posts
If you write a trigger in the DB, OK, as long as the ID is deleted, then all the data of ParentID = ID will be automatically deleted! Delivery rule is definitely necessary! This is because there are multiple directories!

public OleDbConnection cn = new OleDbConnection ();
public bool blnFirstLayer = true;
public ArrayList aryAllChildID = new ArrayList ();
public string [] strAllChildID;

public void GetAllChildID (string paID)
{
    // Put each ID here!
     aryAllChildID.Add (paID)
    // Save this top-level ID
    string strRootID = paID;
    // Store SQL statement
    string sql = "SELECT ID FROM TABLE_NAME WHERE PARENTID = '" + paID + "'";
    cn.ConnectionString = "Provider = ...................";
    cn.Open ();
    OleDbCommand oCmd = new OleDbCommand (sql, cn);
    OleDbDataReader oDr = new OleDbDataReader ();
    oDr = oCmd.ExecuteReader (CommandBehavior.CloseConnection);
    if (oDr.HasRows)
    {
         while (oDr.Read ())
{
string strTmpID = Convert.ToString ((oDr ["ID"] == System.DBNull.Value)? String.Empty: oDr ["ID"]);
GetAllChildID (strTmpID);
}
     }
}


It may be a bit wrong without testing! Please debug by yourself! Ha ha
Reply

Use magic Report

0

Threads

16

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-5-4 21:30:01
| Show all posts
When displaying the tree directory, you can set the Tag or name attribute of the node to the value of your id. Take the tag as an example. When deleting, you can execute "delete * from where id =" + (int) Node.Tag and where parentid = "+ (int) Node.Tag will do
Reply

Use magic Report

1

Threads

26

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-5-5 20:15:01
| Show all posts
I was in a hurry, some garbage code, now remove those useless codes!
You can take another look and add some notes!

// Declare the DB connection object
public OleDbConnection cn = new OleDbConnection ();

// After executing the following function, the value here is all the ChildID you want !!!
public ArrayList aryAllChildID = new ArrayList ();


public void GetAllChildID (string paID)
{
    // Put each ID here!
     aryAllChildID.Add (paID)
    // Of course, here you can delete the DB directly according to the incoming ID! Remember to write down this ID, and use it to find out if it has a sub-ID!
    .......... delete from table_name where id = paid
    ..........
    string strTmpRootID = paID; // No longer delete here, don't need this sentence!
    // Store SQL statement
    string sql = "SELECT ID FROM TABLE_NAME WHERE PARENTID = '" + paID + "'";
    cn.ConnectionString = "Provider = ...................";
    // Open the connection
    cn.Open ();
    // Declare the Command object
    OleDbCommand oCmd = new OleDbCommand (sql, cn);
    // Declare to store the returned data set object
    OleDbDataReader oDr = new OleDbDataReader ();
    // Get the return data
    oDr = oCmd.ExecuteReader (CommandBehavior.CloseConnection);
    if (oDr.HasRows)
    {
         while (oDr.Read ())
{
string strTmpID = Convert.ToString ((oDr ["ID"] == System.DBNull.Value)? String.Empty: oDr ["ID"]);
              // If it is not empty, iterate. If your DB defines id + parentid as PK, you don't need the following conditions to judge!
              if (strTmpID! = String.Empty)
              {
GetAllChildID (strTmpID);
              }
}
     }
}
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Invalid IP Address

Post time: 2020-5-7 22:30:02
| Show all posts
Come the easiest
Using triggers, delete table where parentid in (select id from deleted)
Reply

Use magic Report

4

Threads

12

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 Colombia

 Author| Post time: 2020-5-16 19:00:02
| Show all posts
Thank you all, go back and debug.
Reply

Use magic Report

4

Threads

12

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

 Author| Post time: 2020-6-21 13:15:01
| Show all posts
Thank you, I don’t know much about triggers and complicated stored procedures. I used the following code to write a recursion, and then the IDs of the child nodes are stored in the ArrayList, and then delete the record in the for loop is OK.

//Get all child node IDs
private void GetAllChildNodeId(int parentid)
    {
        idArrayList.Add(parentid);
        SqlConnection getConn = new SqlConnection("server=localhost;database=personmanager;uid=sa;pwd=sa");
        SqlCommand getCmd = getConn.CreateCommand();
        getCmd.CommandText = "select id from persongroup where parentid=" + parentid;
        SqlDataAdapter getDa = new SqlDataAdapter(getCmd);
        DataSet getDs = new DataSet();
        getConn.Open();
        getDa.Fill(getDs, "id");
        getConn.Close();
        foreach (DataRow getDr in getDs.Tables["id"].Rows)
            {
                int newParentid = (int)getDr["id"];
                idArrayList.Add(newParentid);
                GetAllChildNodeId(newParentid);
            }
    }
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list