delphi7 treeview + node maintains a dynamic database

First, that the basic structure under the tree node corresponding table, it is necessary to have the field (node number, the parent node number, node name), add additional fields as needed you develop
starting to add nodes, remove the outset table maximum node No, every time you add a node when the node number is increased by 1;
when we add nodes have two steps, the first is to establish a node treeview species; followed by adding the appropriate node records to the database;
time to add nodes of the tree, it should be noted that node numbers should be added to the data property of the node.
when added node records in the database, the parent node numbers and node numbers less, if 0 is the first tier nodes, then the node parent Id is '0' (in which case you define yourself, I'm here to '0')
to display node I wrote a generic function to achieve the
deletion of nodes due to remove the node and all its descendant nodes in the table the record, this statement does not seem any good sql server, I wrote a stored procedure to achieve

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ShellAPI, DB, ADODB;

type
  TForm1 = class(TForm)
    tv1: TTreeView;
    btn_addtj: TButton;
    btn_addzjd: TButton;
    btn_del: TButton;
    edt_jdmc: TEdit;
    Label1: TLabel;
    btn_xs: TButton;
    con1: TADOConnection;
    qry1: TADOQuery;
    qry2: TADOQuery;
    procedure btn_addtjClick(Sender: TObject);
    procedure btn_addzjdClick(Sender: TObject);
    procedure btn_delClick(Sender: TObject);
    procedure btn_xsClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  maxdwbh: integer;
procedure addChildNodes(tv: TTreeView; fnode: TTreeNode; qry: TADOQuery); overload;
procedure addChildNodes(tv: TTreeView; fnode: TTreeNode; qry: TADOQuery; tab, jdbh, jdmc, fjdbh, nilfjdbh: string); overload;
implementation

{R & lt *. Dfm $ } 
{ 
function: Displays a treeview parent-child node, and the node number and store their data property 
parameters: tv display tree 
     fnode tv represents a node, the node is usually the first call take nil 
     qry a ADOQuery control, used to make queries 
     tab database table name of our father and son nodes stored 
     jdbh tab in the node number represented by the field name 
     jdmc tab in the name of the field represented by node name 
     field name fjdbh tab in the parent node number represented 
     nilfjdbh parent tree node field 0th to the assigned value 
Note: we tree in addition (child) node, when it should be the number of nodes stored in the node to the data property 
} 

Procedure addChildNodes (TV: TTreeView; FNODE: TTreeNode; the qry: TADOQuery; Tab, jdbh, JDMC, fjdbh, nilfjdbh: String );
 var 
  SQL, fdwbh: String ; 
  Node, newNode: TTreeNode; 
  pinteger: ^ String ;
the begin 
  // determination table has no node, even if the 0th node does not exist, we would not need the display tree 
  SQL: = ' SELECT * WHERE fjdbh from Tab =' ' ' + nilfjdbh + ' ' ' ' ; 
  qry.Close; 
  qry.SQL.Clear; 
  qry.SQL.Add (SQL); 
  qry.Open; 
  IF  Not qry.IsEmpty the then 
  the begin 
    IF FNODE = nil  the then 
      fdwbh: = nilfjdbh
     the else 
      fdwbh: = String (fnode.data ^) ;
     // record of all child nodes of the removed node fnode 
    SQL: = ' SELECT ' + + jdbh' , ' + JDMC + ' , ' + fjdbh + ' from ' + Tab + ' WHERE ' + fjdbh + ' =' ' ' + + fdwbh ' ' 'Order by ' + jdbh; 
    qry.Close; 
    qry.SQL.Clear ; 
    qry.SQL.Add (SQL); 
    qry.Open; 
    // tree child node adding 
    IF  Not qry.IsEmpty the then  // subnode 
    the begin 
      // all child nodes of a node is added 
      qry.First;
       the while  Not the qry.Eof do
      the begin 
        newNode: = tv.Items.AddChild (FNODE, qry.fieldbyname (JDMC) .AsString); 
        New (pinteger); 
        pinteger ^: = qry.fieldbyname (jdbh) .AsString; 
        newnode.Data: = pinteger; 
        qry.Next ; 
      End ;
       // positioned next node of the operation of the 
      IF FNODE = nil  the then 
        node: = tv.Items.GetFirstNode
       the else 
        node: = fnode.getFirstChild;
       // recursive call for the same operation for the next child node 
      addChildNodes (tv, Node, the qry, Tab, jdbh, JDMC, fjdbh, nilfjdbh);
     End the else  // no child nodes taken where its next sibling node recursive call 
    the begin 
      Node: = fnode.GetNext;
       IF Node <> nil  the then 
        addChildNodes (TV, Node, the qry, Tab, jdbh, JDMC, fjdbh, nilfjdbh) ; 
    End ;
   End ;
 End ; 

Procedure addChildNodes (TV: TTreeView; FNODE: TTreeNode; the qry: TADOQuery);
 var 
  fdwbh: Integer; 
  SQL: String ; 
  Node, newNode: TTreeNode; 
  Pdata: ^ Integer; 
the begin 
  IF FNODE = nil  the then 
    fdwbh : = 0 
  the else
    fdwbh: = Integer (fnode.data ^);
   // record of all child nodes of the removed node fnode 
  SQL: = ' SELECT dwbh, dwmc, fdwbh from zzjgbmb WHERE fdwbh = ' + IntToStr (fdwbh) + ' Order by dwbh ' ; 
  qry.Close; 
  qry.SQL.Clear; 
  qry.SQL.Add (SQL); 
  qry.Open; 
  // tree child node adding 
  IF  Not qry.IsEmpty the then  // subnode 
  the begin 
    // all add a child node node 
    qry.First;
     the while  Not qry.Eof do 
    the begin 
      newNode: = tv.Items.AddChild (FNODE, qry.fieldbyname (' Dwmc ' ) .AsString); 
      New (Pdata); 
      Pdata ^: = qry.fieldbyname ( ' dwbh ' ) .AsInteger; 
      newnode.Data: = Pdata; 
      qry.Next; 
    End ;
     // locate the next node of the operation 
    IF FNODE = nil  the then 
      node: = tv.Items.GetFirstNode
     the else 
      node: = fnode.getFirstChild;
     // recursive call for the same operation on the next node 
    addChildNodes (TV, node, the qry);
   End  the else  // no child nodes case 
  the begin 
    the Node:= fnode.GetNext;
    if node <> nil then
      addChildNodes(tv, node, qry);
  end;
end;


procedure TForm1.btn_addtjClick(Sender: TObject);
var
  jdmc, sql: string;
  fdwbh: integer;
  node, newnode: TTreeNode;
  pinteger: ^integer;
begin
  try
    node := tv1.Selected;
    jdmc := edt_jdmc.text;
    newnode := tv1.Items.add(node, jdmc);
    maxdwbh := maxdwbh + 1;
    New(pinteger);
    pinteger^ := maxdwbh;
    newnode.data := pinteger;

    if node = nil then
      fdwbh := 0
    else if node.Level = 0 then
      fdwbh := 0
    else
      fdwbh := Integer(node.Parent.data^);
    sql := 'insert into zzjgbmb(dwbh,fdwbh,dwmc) values(' + inttostr(maxdwbh) + ',' + inttostr(fdwbh) + ',''' + jdmc + ''')';
    qry1.Close;
    qry1.SQL.Clear;
    qry1.SQL.Add(sql);
    qry1.ExecSQL;
    ShowMessage('单位"' + jdmc + '"添加成功!');
  except
    on e: Exception do
      ShowMessage(e.Message);
  end;
end;

procedure TForm1.btn_addzjdClick(Sender: TObject);
var
  jdmc, sql: string;
  fdwbh: integer;
  node, newnode: TTreeNode;
  pinteger: ^integer;
begin
  try
    jdmc := edt_jdmc.text;
    node := tv1.Selected;
    newnode := tv1.Items.AddChild(node, jdmc);
    maxdwbh := maxdwbh + 1;
    New(pinteger);
    pinteger^ := maxdwbh;
    newnode.Data := pinteger;
    if node = nil then
      Exit
    else
      fdwbh := Integer(node.data^);

    sql := 'insert into zzjgbmb(dwbh,fdwbh,dwmc) values(' + inttostr(maxdwbh) + ',' + inttostr(fdwbh) + ',''' + jdmc + ''')';
    qry1.Close;
    qry1.SQL.Clear;
    qry1.SQL.Add(sql);
    qry1.ExecSQL;
    ShowMessage('单位"' + jdmc + '"添加成功!');
  except
    on e: Exception do
      ShowMessage(e.Message);
  end;
end;

procedure TForm1.btn_delClick(Sender: TObject);
var
  node: TTreeNode;
  dwbh: integer;
  sql: string;
begin
  try
    node := tv1.Selected;
    node.Delete;
    dwbh := Integer(node.data^);
    sql := 'exec p_zzjgbmb_delnode ' + inttostr(dwbh);
    qry1.Close;
    qry1.SQL.Clear;
    qry1.SQL.Add(sql);
    qry1.ExecSQL;
  except
    on e: Exception do
      ShowMessage(e.Message);
  end;
end;

procedure TForm1.btn_xsClick(Sender: TObject);
begin
  try
    tv1.Items.Clear;
    addChildNodes(tv1, nil, qry1, 'zzjgbmb', 'dwbh', 'dwmc', 'fdwbh', '0');
  except
    on e: Exception do
      ShowMessage(e.Message);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  sql: string;
begin
  try
    maxdwbh := 0;
    sql := 'select max(dwbh) maxdwbh from zzjgbmb';
    qry1.Close;
    qry1.SQL.Clear;
    qry1.SQL.Add(sql);
    qry1.Open;
    if not qry1.isempty then
    begin
      qry1.First;
      maxdwbh := qry1.fieldbyname('maxdwbh').AsInteger;
    end;
  except
    on e: Exception do
      showmessage(e.message);
  end;
end;

end.

Stored procedure to remove the node and its descendant nodes

create procedure [dbo].[p_zzjgbmb_delnode](@dwbh int)
as
begin
  declare @tmp1 table (dwbh int)
  declare @tmp2 table (dwbh int)
  declare @tmp3 table (dwbh int)
  
  insert into @tmp1 select dwbh from zzjgbmb where fdwbh=@dwbh
  insert into @tmp3 select dwbh from @tmp1
  
  while exists(select dwbh from zzjgbmb where fdwbh in (select dwbh from @tmp1))
  begin
    delete @tmp2
    insert into @tmp2 select dwbh from zzjgbmb where fdwbh in (select dwbh from @tmp1)    
    delete @tmp1
    insert into @tmp1 select dwbh from @tmp2   
    insert into @tmp3 select dwbh from @tmp1 
  end 
  
  delete from zzjgbmb where dwbh in (select dwbh from @tmp3); 
  delete from zzjgbmb where dwbh = @dwbh
end

Simple interface

 

Guess you like

Origin www.cnblogs.com/jijm123/p/11365674.html