Y
yirong
Unregistered / Unconfirmed
GUEST, unregistred user!
我原来用ADO连接ACCESS数据库建立树型结构已正常使用,源码如下TREE_OLD.PAS)
因我现要用INTEBASE数据库,无INTERBASE ODBC驱动,我想把TREE_OLD.PAS改成TREE_NEW.PAS
源码如tree_new.pas,可在运行过程中出现:*.EXE raised exception EDBengineError with message"General SQL error unexpected end of end" ,
各位帮我看看,先谢啦.
1.TREE_OLD.PAS
unit tree_old;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ImgList, ComCtrls, DB, ADODB, StdCtrls;
type
Told_tree = class(TForm)
ImageList1: TImageList;
TreeView1: TTreeView;
ADOConnection1: TADOConnection;
EdtId: TEdit;
EdtName: TEdit;
EdtFid: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
BtnSave: TButton;
BtnClear: TButton;
BtnEdit: TButton;
BtnDelete: TButton;
BtnAdd: TButton;
BtnExit: TButton;
Button3: TButton;
ADOQ_Tree: TADOQuery;
Function AddNode(TreeView:TTreeView;Node:TTreeNode;ADOQ:TADOQuery):TTreeNode;
Function SearchNode(TreeView:TTreeView;Txts:String):TTreeNode;
Procedure ShowTree(TreeView:TTreeView;ADOQ:TADOQuery);
Procedure BtnEnabled(Btn:Boolean=True);
procedure BtnClearClick(Sender: TObject);
procedure EdtIdExit(Sender: TObject);
procedure EdtNameExit(Sender: TObject);
procedure BtnAddClick(Sender: TObject);
procedure BtnEditClick(Sender: TObject);
procedure BtnSaveClick(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
procedure BtnDeleteClick(Sender: TObject);
procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure CopyNodeUnder(TreeView:TTreeview;SourceNode,TargetNode:TtreeNode);
procedure Button3Click(Sender: TObject);
procedure BtnExitClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Procedure Init();
Procedure Clear_Edt();
Function Exec_SQL(Que:TADOQuery;Text:String;Flag:Boolean=True):Boolean;
end;
var
old_tree: Told_tree;
str,tempstr,tempname:string;
implementation
{$R *.dfm}
Function Told_tree.AddNode(TreeView:TTreeview;Node:TTreeNode;ADOQ:TADOQuery):TTreeNode;
var
Node1:TTreeNode;
sString;
begin
Node1:=TreeView.Items.AddChild(Node,ADOQ.FieldByName('name').Text+'('+
ADOQ.FieldByName('id').Text+')');
New(s);
s^:=ADOQ.FieldByName('id').Text;
Node1.Data:=s;
Result:=Node1;
end;
Function Told_tree.SearchNode(TreeView:TTreeView;Txts:String):TTreeNode;
var
i:integer;
sString;
begin
Result:=nil;
For i:=0 To TreeView.Items.Count-1 Do
begin
s:=TreeView.Items.Data;
If s^=Txts Then
begin
Result:=TreeView.Items;
Exit;
end;
end;
end;
Procedure Told_tree.ShowTree(TreeView:TTreeView;ADOQ:TADOQuery);
var
i,Rec_Num:integer;
Node1,Node2:TTreeNode;
begin
TreeView.Items.Clear;
TreeView.Items.BeginUpdate;
Node1:=TreeView.Items.GetFirstNode;
Exec_SQL(ADOQ,'Select * From Tree_1 Order By pid,id',True);
If ADOQ.RecordCount>0 Then
Begin
Rec_Num:= ADOQ.RecordCount;
For i:=0 To Rec_Num-1 Do
begin
If ADOQ.FieldByName('pid').Text='0' Then
Node2:=AddNode(TreeView,Node1,ADOQ)
Else
begin
Node2:=SearchNode(TreeView,ADOQ.FieldByName('pid').Text);
If Node2<>nil Then
AddNode(TreeView,Node2,ADOQ);
end;
ADOQ.Next;
end;
End;
TreeView.Items.EndUpdate;
end;
Procedure Told_tree.BtnEnabled(Btn:Boolean=True);
begin
BtnSave.Enabled:=not Btn;
BtnAdd.Enabled:=Btn;
Btndelete.Enabled:=Btn;
BtnExit.Enabled:=Btn;
BtnEdit.Enabled:=Btn;
BtnClear.Enabled:=not Btn;
EdtId.Enabled:=not Btn;
EdtName.Enabled:=not Btn;
EdtFid.Enabled:=not Btn;
end;
procedure Told_tree.BtnClearClick(Sender: TObject);
begin
Init();
Clear_Edt;
end;
procedure Told_tree.EdtIdExit(Sender: TObject);
Function Rec:Boolean;
Var
ADOQ:TADOQuery;
begin
ADOQ:=TADOQuery.Create(self);
With ADOQ Do
begin
Connection:=ADOConnection1;
SQL.Text:='select * from tree_1 where id='''+EdtId.Text+'''';
Open;
Result:=RecordCount>0;
Free;
end;
end;
begin
If (Rec) Then
begin
Application.MessageBox('记录已存在请重新输入?', '提示', MB_OK);
EdtId.SelectAll;
end;
end;
procedure Told_tree.EdtNameExit(Sender: TObject);
begin
If (Trim(EdtName.Text)='')And(Trim(Edtid.Text)<>'') then
begin
Application.MessageBox('名称不能为空白!','警告',48);
EdtName.SetFocus;
end;
end;
procedure Told_tree.BtnAddClick(Sender: TObject);
begin
BtnEnabled(False);
str:='增加';
end;
procedure Told_tree.BtnEditClick(Sender: TObject);
var
Sql_Txt:String;
begin
BtnEnabled(False);
str:='修改';
Sql_Txt:='select * from tree_1 where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,True);
Edtid.Text:=ADOQ_Tree.FieldValues['id'];
EdtName.Text:=ADOQ_Tree.FieldValues['name'];
EdtFid.Text:=ADOQ_Tree.FieldValues[' pid'];
end;
procedure Told_tree.BtnSaveClick(Sender: TObject);
var
Sql_Txt:string;
begin
If EdtFid.Text='' then EdtFid.Text:='0';
If str='增加' Then
Try
Sql_Txt:='insert into tree_1 values('''+Edtid.Text
+''','''+Edtname.Text+''','''+EdtFid.Text+''')';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
Except
Application.MessageBox('记录增加失败!!!','提示信息',0);
end;
If str='修改' Then
Try
Sql_Txt:='update tree_1 set id='''+Edtid.Text
+''',name='''+Edtname.Text+''',pid='''+EdtFid.Text
+''' Where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
Except
Application.MessageBox('记录修改失败!!!','提示信息',0);
end;
Button3Click(nil);
end;
procedure Told_tree.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
PidString;
Sql_Txt:String;
begin
Pid:=Node.Data;
Sql_Txt:= 'select * from tree_1 where id='''+Pid^+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,True);
tempstr:=ADOQ_Tree.FieldByName('id').Text;
tempname:=ADOQ_Tree.FieldByName('name').Text;
end;
procedure Told_tree.BtnDeleteClick(Sender: TObject);
var
Sql_Txt:String;
begin
If MessageDlg('你确认删除该记录吗?',mtWarning,[mbYES,mbNO],0)=mrYES then
try
Sql_Txt:= 'delete from tree_1 where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
Init();
Except
Application.MessageBox('删除失败!!!','提示',0);
end;
end;
procedure Told_tree.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var
TargetNode,SourceNode:TTreeNode;
begin
TargetNode:=TreeView1.GetNodeAt(X,Y);
If (Source=Sender) and (TargetNode<>nil) then
begin
Accept:=True;
SourceNode:=TreeView1.Selected;
while (TargetNode.Parent<>nil) and (TargetNode <> SourceNode) do
TargetNode:=TargetNode.Parent;
if (TargetNode = SourceNode) then Accept:=False;
end
else Accept:=False;
end;
procedure Told_tree.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
targetnode,sourcenode:TTreenode;
Pstrstring;
Sql_Txt:String;
begin
TargetNode:=TreeView1.GetNodeAt(x,y);
SourceNode:=TreeView1.Selected;
Pstr:=TargetNode.Data;
Sql_Txt:= 'update tree_1 set pid='''+Pstr^+''' where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
TreeView1.Items.BeginUpdate;
try
CopyNodeUnder(TreeView1,SourceNode,TargetNode);
TreeView1.Selected:=TargetNode;
finally
TreeView1.Items.EndUpdate;
end;
end;
procedure Told_tree.CopyNodeUnder(TreeView:TTreeView;SourceNode,TargetNode:TtreeNode);
var
Node:TTreeNode;
i:integer;
begin
Node:=TreeView.Items.AddChildFirst(TargetNode,'');
Node.Assign(SourceNode);
for i:=SourceNode.Count-1 downto 0 do
begin
CopyNodeUnder(Treeview,SourceNode.Item,Node);
end;
TreeView.Items.Delete(SourceNode);
end;
procedure Told_tree.Init;
begin
ShowTree(TreeView1,ADOQ_Tree);
end;
procedure Told_tree.Button3Click(Sender: TObject);
begin
Init();
Clear_Edt;
end;
procedure Told_tree.BtnExitClick(Sender: TObject);
begin
Close;
end;
procedure Told_tree.Clear_Edt;
begin
EdtId.Text:='';
EdtName.Text:='';
EdtFid.Text:='';
str:='';
tempstr:='';
BtnEnabled();
end;
Function Told_tree.Exec_SQL(Que:TADOQuery;Text:String;Flag:Boolean=True):Boolean;
Begin
With Que Do
begin
Close;
Sql.Clear;
Sql.Add(Text);
If Flag Then
Open
Else
ExecSQL;
end;
End;
end.
2.TREE_NEW.PAS
unit TREE_new;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ImgList, ComCtrls, DB, ADODB, StdCtrls, DBTables;
type
Tnew_tree = class(TForm)
ImageList1: TImageList;
TreeView1: TTreeView;
EdtId: TEdit;
EdtName: TEdit;
EdtFid: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
BtnSave: TButton;
BtnClear: TButton;
BtnEdit: TButton;
BtnDelete: TButton;
BtnAdd: TButton;
BtnExit: TButton;
Button3: TButton;
GFTConnection1: TDatabase;
ADOQ_Tree: TQuery;
Function AddNode(TreeView:TTreeView;Node:TTreeNode;ADOQ:TQuery):TTreeNode;
Function SearchNode(TreeView:TTreeView;Txts:String):TTreeNode;
Procedure ShowTree(TreeView:TTreeView;ADOQ:TQuery);
Procedure BtnEnabled(Btn:Boolean=True);
procedure BtnClearClick(Sender: TObject);
procedure EdtIdExit(Sender: TObject);
procedure EdtNameExit(Sender: TObject);
procedure BtnAddClick(Sender: TObject);
procedure BtnEditClick(Sender: TObject);
procedure BtnSaveClick(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
procedure BtnDeleteClick(Sender: TObject);
procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure CopyNodeUnder(TreeView:TTreeview;SourceNode,TargetNode:TtreeNode);
procedure Button3Click(Sender: TObject);
procedure BtnExitClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Procedure Init();
Procedure Clear_Edt();
Function Exec_SQL(Que:TQuery;Text:String;Flag:Boolean=True):Boolean; //运行时不认
end;
var
new_tree: Tnew_tree;
str,tempstr,tempname:string;
implementation
{$R *.dfm}
Function Tnew_tree.AddNode(TreeView:TTreeview;Node:TTreeNode;ADOQ:TQuery):TTreeNode;
var
Node1:TTreeNode;
sString;
begin
Node1:=TreeView.Items.AddChild(Node,ADOQ.FieldByName('name').Text+'('+
ADOQ.FieldByName('id').Text+')');
New(s);
s^:=ADOQ.FieldByName('id').Text;
Node1.Data:=s;
Result:=Node1;
end;
Function Tnew_tree.SearchNode(TreeView:TTreeView;Txts:String):TTreeNode;
var
i:integer;
sString;
begin
Result:=nil;
For i:=0 To TreeView.Items.Count-1 Do
begin
s:=TreeView.Items.Data;
If s^=Txts Then
begin
Result:=TreeView.Items;
Exit;
end;
end;
end;
Procedure Tnew_tree.ShowTree(TreeView:TTreeView;ADOQ:TQuery);
var
i,Rec_Num:integer;
Node1,Node2:TTreeNode;
begin
TreeView.Items.Clear;
TreeView.Items.BeginUpdate;
Node1:=TreeView.Items.GetFirstNode;
Exec_SQL(ADOQ,'Select * From product_type Order By pid,id',True);
If ADOQ.RecordCount>0 Then
Begin
Rec_Num:= ADOQ.RecordCount;
For i:=0 To Rec_Num-1 Do
begin
If ADOQ.FieldByName('pid').Text='0' Then
Node2:=AddNode(TreeView,Node1,ADOQ)
Else
begin
Node2:=SearchNode(TreeView,ADOQ.FieldByName('pid').Text);
If Node2<>nil Then
AddNode(TreeView,Node2,ADOQ);
end;
ADOQ.Next;
end;
End;
TreeView.Items.EndUpdate;
end;
Procedure Tnew_tree.BtnEnabled(Btn:Boolean=True);
begin
BtnSave.Enabled:=not Btn;
BtnAdd.Enabled:=Btn;
Btndelete.Enabled:=Btn;
BtnExit.Enabled:=Btn;
BtnEdit.Enabled:=Btn;
BtnClear.Enabled:=not Btn;
EdtId.Enabled:=not Btn;
EdtName.Enabled:=not Btn;
EdtFid.Enabled:=not Btn;
end;
procedure Tnew_tree.BtnClearClick(Sender: TObject);
begin
Init();
Clear_Edt;
end;
procedure Tnew_tree.EdtIdExit(Sender: TObject);
Function Rec:Boolean;
Var
ADOQ:TQuery;
begin
ADOQ:=TQuery.Create(self);
With ADOQ Do
begin
DATABASENAME:=GFTConnection1.databasename;
SQL.Text:='select * from product_type where id='''+EdtId.Text+'''';
Open;
Result:=RecordCount>0;
Free;
end;
end;
begin
If (Rec) Then
begin
Application.MessageBox('记录已存在请重新输入?', '提示', MB_OK);
EdtId.SelectAll;
end;
end;
procedure Tnew_tree.EdtNameExit(Sender: TObject);
begin
If (Trim(EdtName.Text)='')And(Trim(Edtid.Text)<>'') then
begin
Application.MessageBox('名称不能为空白!','警告',48);
EdtName.SetFocus;
end;
end;
procedure Tnew_tree.BtnAddClick(Sender: TObject);
begin
BtnEnabled(False);
str:='增加';
end;
procedure Tnew_tree.BtnEditClick(Sender: TObject);
var
Sql_Txt:String;
begin
BtnEnabled(False);
str:='修改';
Sql_Txt:='select * from product_type where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,True);
Edtid.Text:=ADOQ_Tree.FieldValues['id'];
EdtName.Text:=ADOQ_Tree.FieldValues['name'];
EdtFid.Text:=ADOQ_Tree.FieldValues['pid'];
end;
procedure Tnew_tree.BtnSaveClick(Sender: TObject);
var
Sql_Txt:string;
begin
If EdtFid.Text='' then EdtFid.Text:='0';
If str='增加' Then
Try
Sql_Txt:='insert into product_type values('''+Edtid.Text
+''','''+Edtname.Text+''','''+EdtFid.Text+''')';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
Except
Application.MessageBox('记录增加失败!!!','提示信息',0);
end;
If str='修改' Then
Try
Sql_Txt:='update product_type set id='''+Edtid.Text
+''',name='''+Edtname.Text+''',pid='''+EdtFid.Text
+''' Where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
Except
Application.MessageBox('记录修改失败!!!','提示信息',0);
end;
Button3Click(nil);
end;
procedure Tnew_tree.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
PidString;
Sql_Txt:String;
begin
Pid:=Node.Data;
Sql_Txt:= 'select * from product_type where id='''+Pid^+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,True);
tempstr:=ADOQ_Tree.FieldByName('id').Text;
tempname:=ADOQ_Tree.FieldByName('name').Text;
end;
procedure Tnew_tree.BtnDeleteClick(Sender: TObject);
var
Sql_Txt:String;
begin
If MessageDlg('你确认删除该记录吗?',mtWarning,[mbYES,mbNO],0)=mrYES then
try
Sql_Txt:= 'delete from product_type where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
Init();
Except
Application.MessageBox('删除失败!!!','提示',0);
end;
end;
procedure Tnew_tree.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var
TargetNode,SourceNode:TTreeNode;
begin
TargetNode:=TreeView1.GetNodeAt(X,Y);
If (Source=Sender) and (TargetNode<>nil) then
begin
Accept:=True;
SourceNode:=TreeView1.Selected;
while (TargetNode.Parent<>nil) and (TargetNode <> SourceNode) do
TargetNode:=TargetNode.Parent;
if (TargetNode = SourceNode) then Accept:=False;
end
else Accept:=False;
end;
procedure Tnew_tree.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
targetnode,sourcenode:TTreenode;
Pstrstring;
Sql_Txt:String;
begin
TargetNode:=TreeView1.GetNodeAt(x,y);
SourceNode:=TreeView1.Selected;
Pstr:=TargetNode.Data;
Sql_Txt:= 'update product_type set pid='''+Pstr^+''' where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
TreeView1.Items.BeginUpdate;
try
CopyNodeUnder(TreeView1,SourceNode,TargetNode);
TreeView1.Selected:=TargetNode;
finally
TreeView1.Items.EndUpdate;
end;
end;
procedure Tnew_tree.CopyNodeUnder(TreeView:TTreeView;SourceNode,TargetNode:TtreeNode);
var
Node:TTreeNode;
i:integer;
begin
Node:=TreeView.Items.AddChildFirst(TargetNode,'');
Node.Assign(SourceNode);
for i:=SourceNode.Count-1 downto 0 do
begin
CopyNodeUnder(Treeview,SourceNode.Item,Node);
end;
TreeView.Items.Delete(SourceNode);
end;
procedure Tnew_tree.Init;
begin
ShowTree(TreeView1,ADOQ_Tree);
end;
procedure Tnew_tree.Button3Click(Sender: TObject);
begin
Init();
Clear_Edt;
end;
procedure Tnew_tree.BtnExitClick(Sender: TObject);
begin
Close;
end;
procedure Tnew_tree.Clear_Edt;
begin
EdtId.Text:='';
EdtName.Text:='';
EdtFid.Text:='';
str:='';
tempstr:='';
BtnEnabled();
end;
Function Tnew_tree.Exec_SQL(Que:TQuery;Text:String;Flag:Boolean=True):Boolean;
Begin
With Que Do
begin
Close;
Sql.Clear;
Sql.Add(Text);
If Flag Then
Open
Else
ExecSQL;
end;
End;
end.
因我现要用INTEBASE数据库,无INTERBASE ODBC驱动,我想把TREE_OLD.PAS改成TREE_NEW.PAS
源码如tree_new.pas,可在运行过程中出现:*.EXE raised exception EDBengineError with message"General SQL error unexpected end of end" ,
各位帮我看看,先谢啦.
1.TREE_OLD.PAS
unit tree_old;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ImgList, ComCtrls, DB, ADODB, StdCtrls;
type
Told_tree = class(TForm)
ImageList1: TImageList;
TreeView1: TTreeView;
ADOConnection1: TADOConnection;
EdtId: TEdit;
EdtName: TEdit;
EdtFid: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
BtnSave: TButton;
BtnClear: TButton;
BtnEdit: TButton;
BtnDelete: TButton;
BtnAdd: TButton;
BtnExit: TButton;
Button3: TButton;
ADOQ_Tree: TADOQuery;
Function AddNode(TreeView:TTreeView;Node:TTreeNode;ADOQ:TADOQuery):TTreeNode;
Function SearchNode(TreeView:TTreeView;Txts:String):TTreeNode;
Procedure ShowTree(TreeView:TTreeView;ADOQ:TADOQuery);
Procedure BtnEnabled(Btn:Boolean=True);
procedure BtnClearClick(Sender: TObject);
procedure EdtIdExit(Sender: TObject);
procedure EdtNameExit(Sender: TObject);
procedure BtnAddClick(Sender: TObject);
procedure BtnEditClick(Sender: TObject);
procedure BtnSaveClick(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
procedure BtnDeleteClick(Sender: TObject);
procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure CopyNodeUnder(TreeView:TTreeview;SourceNode,TargetNode:TtreeNode);
procedure Button3Click(Sender: TObject);
procedure BtnExitClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Procedure Init();
Procedure Clear_Edt();
Function Exec_SQL(Que:TADOQuery;Text:String;Flag:Boolean=True):Boolean;
end;
var
old_tree: Told_tree;
str,tempstr,tempname:string;
implementation
{$R *.dfm}
Function Told_tree.AddNode(TreeView:TTreeview;Node:TTreeNode;ADOQ:TADOQuery):TTreeNode;
var
Node1:TTreeNode;
sString;
begin
Node1:=TreeView.Items.AddChild(Node,ADOQ.FieldByName('name').Text+'('+
ADOQ.FieldByName('id').Text+')');
New(s);
s^:=ADOQ.FieldByName('id').Text;
Node1.Data:=s;
Result:=Node1;
end;
Function Told_tree.SearchNode(TreeView:TTreeView;Txts:String):TTreeNode;
var
i:integer;
sString;
begin
Result:=nil;
For i:=0 To TreeView.Items.Count-1 Do
begin
s:=TreeView.Items.Data;
If s^=Txts Then
begin
Result:=TreeView.Items;
Exit;
end;
end;
end;
Procedure Told_tree.ShowTree(TreeView:TTreeView;ADOQ:TADOQuery);
var
i,Rec_Num:integer;
Node1,Node2:TTreeNode;
begin
TreeView.Items.Clear;
TreeView.Items.BeginUpdate;
Node1:=TreeView.Items.GetFirstNode;
Exec_SQL(ADOQ,'Select * From Tree_1 Order By pid,id',True);
If ADOQ.RecordCount>0 Then
Begin
Rec_Num:= ADOQ.RecordCount;
For i:=0 To Rec_Num-1 Do
begin
If ADOQ.FieldByName('pid').Text='0' Then
Node2:=AddNode(TreeView,Node1,ADOQ)
Else
begin
Node2:=SearchNode(TreeView,ADOQ.FieldByName('pid').Text);
If Node2<>nil Then
AddNode(TreeView,Node2,ADOQ);
end;
ADOQ.Next;
end;
End;
TreeView.Items.EndUpdate;
end;
Procedure Told_tree.BtnEnabled(Btn:Boolean=True);
begin
BtnSave.Enabled:=not Btn;
BtnAdd.Enabled:=Btn;
Btndelete.Enabled:=Btn;
BtnExit.Enabled:=Btn;
BtnEdit.Enabled:=Btn;
BtnClear.Enabled:=not Btn;
EdtId.Enabled:=not Btn;
EdtName.Enabled:=not Btn;
EdtFid.Enabled:=not Btn;
end;
procedure Told_tree.BtnClearClick(Sender: TObject);
begin
Init();
Clear_Edt;
end;
procedure Told_tree.EdtIdExit(Sender: TObject);
Function Rec:Boolean;
Var
ADOQ:TADOQuery;
begin
ADOQ:=TADOQuery.Create(self);
With ADOQ Do
begin
Connection:=ADOConnection1;
SQL.Text:='select * from tree_1 where id='''+EdtId.Text+'''';
Open;
Result:=RecordCount>0;
Free;
end;
end;
begin
If (Rec) Then
begin
Application.MessageBox('记录已存在请重新输入?', '提示', MB_OK);
EdtId.SelectAll;
end;
end;
procedure Told_tree.EdtNameExit(Sender: TObject);
begin
If (Trim(EdtName.Text)='')And(Trim(Edtid.Text)<>'') then
begin
Application.MessageBox('名称不能为空白!','警告',48);
EdtName.SetFocus;
end;
end;
procedure Told_tree.BtnAddClick(Sender: TObject);
begin
BtnEnabled(False);
str:='增加';
end;
procedure Told_tree.BtnEditClick(Sender: TObject);
var
Sql_Txt:String;
begin
BtnEnabled(False);
str:='修改';
Sql_Txt:='select * from tree_1 where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,True);
Edtid.Text:=ADOQ_Tree.FieldValues['id'];
EdtName.Text:=ADOQ_Tree.FieldValues['name'];
EdtFid.Text:=ADOQ_Tree.FieldValues[' pid'];
end;
procedure Told_tree.BtnSaveClick(Sender: TObject);
var
Sql_Txt:string;
begin
If EdtFid.Text='' then EdtFid.Text:='0';
If str='增加' Then
Try
Sql_Txt:='insert into tree_1 values('''+Edtid.Text
+''','''+Edtname.Text+''','''+EdtFid.Text+''')';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
Except
Application.MessageBox('记录增加失败!!!','提示信息',0);
end;
If str='修改' Then
Try
Sql_Txt:='update tree_1 set id='''+Edtid.Text
+''',name='''+Edtname.Text+''',pid='''+EdtFid.Text
+''' Where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
Except
Application.MessageBox('记录修改失败!!!','提示信息',0);
end;
Button3Click(nil);
end;
procedure Told_tree.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
PidString;
Sql_Txt:String;
begin
Pid:=Node.Data;
Sql_Txt:= 'select * from tree_1 where id='''+Pid^+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,True);
tempstr:=ADOQ_Tree.FieldByName('id').Text;
tempname:=ADOQ_Tree.FieldByName('name').Text;
end;
procedure Told_tree.BtnDeleteClick(Sender: TObject);
var
Sql_Txt:String;
begin
If MessageDlg('你确认删除该记录吗?',mtWarning,[mbYES,mbNO],0)=mrYES then
try
Sql_Txt:= 'delete from tree_1 where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
Init();
Except
Application.MessageBox('删除失败!!!','提示',0);
end;
end;
procedure Told_tree.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var
TargetNode,SourceNode:TTreeNode;
begin
TargetNode:=TreeView1.GetNodeAt(X,Y);
If (Source=Sender) and (TargetNode<>nil) then
begin
Accept:=True;
SourceNode:=TreeView1.Selected;
while (TargetNode.Parent<>nil) and (TargetNode <> SourceNode) do
TargetNode:=TargetNode.Parent;
if (TargetNode = SourceNode) then Accept:=False;
end
else Accept:=False;
end;
procedure Told_tree.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
targetnode,sourcenode:TTreenode;
Pstrstring;
Sql_Txt:String;
begin
TargetNode:=TreeView1.GetNodeAt(x,y);
SourceNode:=TreeView1.Selected;
Pstr:=TargetNode.Data;
Sql_Txt:= 'update tree_1 set pid='''+Pstr^+''' where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
TreeView1.Items.BeginUpdate;
try
CopyNodeUnder(TreeView1,SourceNode,TargetNode);
TreeView1.Selected:=TargetNode;
finally
TreeView1.Items.EndUpdate;
end;
end;
procedure Told_tree.CopyNodeUnder(TreeView:TTreeView;SourceNode,TargetNode:TtreeNode);
var
Node:TTreeNode;
i:integer;
begin
Node:=TreeView.Items.AddChildFirst(TargetNode,'');
Node.Assign(SourceNode);
for i:=SourceNode.Count-1 downto 0 do
begin
CopyNodeUnder(Treeview,SourceNode.Item,Node);
end;
TreeView.Items.Delete(SourceNode);
end;
procedure Told_tree.Init;
begin
ShowTree(TreeView1,ADOQ_Tree);
end;
procedure Told_tree.Button3Click(Sender: TObject);
begin
Init();
Clear_Edt;
end;
procedure Told_tree.BtnExitClick(Sender: TObject);
begin
Close;
end;
procedure Told_tree.Clear_Edt;
begin
EdtId.Text:='';
EdtName.Text:='';
EdtFid.Text:='';
str:='';
tempstr:='';
BtnEnabled();
end;
Function Told_tree.Exec_SQL(Que:TADOQuery;Text:String;Flag:Boolean=True):Boolean;
Begin
With Que Do
begin
Close;
Sql.Clear;
Sql.Add(Text);
If Flag Then
Open
Else
ExecSQL;
end;
End;
end.
2.TREE_NEW.PAS
unit TREE_new;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ImgList, ComCtrls, DB, ADODB, StdCtrls, DBTables;
type
Tnew_tree = class(TForm)
ImageList1: TImageList;
TreeView1: TTreeView;
EdtId: TEdit;
EdtName: TEdit;
EdtFid: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
BtnSave: TButton;
BtnClear: TButton;
BtnEdit: TButton;
BtnDelete: TButton;
BtnAdd: TButton;
BtnExit: TButton;
Button3: TButton;
GFTConnection1: TDatabase;
ADOQ_Tree: TQuery;
Function AddNode(TreeView:TTreeView;Node:TTreeNode;ADOQ:TQuery):TTreeNode;
Function SearchNode(TreeView:TTreeView;Txts:String):TTreeNode;
Procedure ShowTree(TreeView:TTreeView;ADOQ:TQuery);
Procedure BtnEnabled(Btn:Boolean=True);
procedure BtnClearClick(Sender: TObject);
procedure EdtIdExit(Sender: TObject);
procedure EdtNameExit(Sender: TObject);
procedure BtnAddClick(Sender: TObject);
procedure BtnEditClick(Sender: TObject);
procedure BtnSaveClick(Sender: TObject);
procedure TreeView1Change(Sender: TObject; Node: TTreeNode);
procedure BtnDeleteClick(Sender: TObject);
procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure CopyNodeUnder(TreeView:TTreeview;SourceNode,TargetNode:TtreeNode);
procedure Button3Click(Sender: TObject);
procedure BtnExitClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Procedure Init();
Procedure Clear_Edt();
Function Exec_SQL(Que:TQuery;Text:String;Flag:Boolean=True):Boolean; //运行时不认
end;
var
new_tree: Tnew_tree;
str,tempstr,tempname:string;
implementation
{$R *.dfm}
Function Tnew_tree.AddNode(TreeView:TTreeview;Node:TTreeNode;ADOQ:TQuery):TTreeNode;
var
Node1:TTreeNode;
sString;
begin
Node1:=TreeView.Items.AddChild(Node,ADOQ.FieldByName('name').Text+'('+
ADOQ.FieldByName('id').Text+')');
New(s);
s^:=ADOQ.FieldByName('id').Text;
Node1.Data:=s;
Result:=Node1;
end;
Function Tnew_tree.SearchNode(TreeView:TTreeView;Txts:String):TTreeNode;
var
i:integer;
sString;
begin
Result:=nil;
For i:=0 To TreeView.Items.Count-1 Do
begin
s:=TreeView.Items.Data;
If s^=Txts Then
begin
Result:=TreeView.Items;
Exit;
end;
end;
end;
Procedure Tnew_tree.ShowTree(TreeView:TTreeView;ADOQ:TQuery);
var
i,Rec_Num:integer;
Node1,Node2:TTreeNode;
begin
TreeView.Items.Clear;
TreeView.Items.BeginUpdate;
Node1:=TreeView.Items.GetFirstNode;
Exec_SQL(ADOQ,'Select * From product_type Order By pid,id',True);
If ADOQ.RecordCount>0 Then
Begin
Rec_Num:= ADOQ.RecordCount;
For i:=0 To Rec_Num-1 Do
begin
If ADOQ.FieldByName('pid').Text='0' Then
Node2:=AddNode(TreeView,Node1,ADOQ)
Else
begin
Node2:=SearchNode(TreeView,ADOQ.FieldByName('pid').Text);
If Node2<>nil Then
AddNode(TreeView,Node2,ADOQ);
end;
ADOQ.Next;
end;
End;
TreeView.Items.EndUpdate;
end;
Procedure Tnew_tree.BtnEnabled(Btn:Boolean=True);
begin
BtnSave.Enabled:=not Btn;
BtnAdd.Enabled:=Btn;
Btndelete.Enabled:=Btn;
BtnExit.Enabled:=Btn;
BtnEdit.Enabled:=Btn;
BtnClear.Enabled:=not Btn;
EdtId.Enabled:=not Btn;
EdtName.Enabled:=not Btn;
EdtFid.Enabled:=not Btn;
end;
procedure Tnew_tree.BtnClearClick(Sender: TObject);
begin
Init();
Clear_Edt;
end;
procedure Tnew_tree.EdtIdExit(Sender: TObject);
Function Rec:Boolean;
Var
ADOQ:TQuery;
begin
ADOQ:=TQuery.Create(self);
With ADOQ Do
begin
DATABASENAME:=GFTConnection1.databasename;
SQL.Text:='select * from product_type where id='''+EdtId.Text+'''';
Open;
Result:=RecordCount>0;
Free;
end;
end;
begin
If (Rec) Then
begin
Application.MessageBox('记录已存在请重新输入?', '提示', MB_OK);
EdtId.SelectAll;
end;
end;
procedure Tnew_tree.EdtNameExit(Sender: TObject);
begin
If (Trim(EdtName.Text)='')And(Trim(Edtid.Text)<>'') then
begin
Application.MessageBox('名称不能为空白!','警告',48);
EdtName.SetFocus;
end;
end;
procedure Tnew_tree.BtnAddClick(Sender: TObject);
begin
BtnEnabled(False);
str:='增加';
end;
procedure Tnew_tree.BtnEditClick(Sender: TObject);
var
Sql_Txt:String;
begin
BtnEnabled(False);
str:='修改';
Sql_Txt:='select * from product_type where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,True);
Edtid.Text:=ADOQ_Tree.FieldValues['id'];
EdtName.Text:=ADOQ_Tree.FieldValues['name'];
EdtFid.Text:=ADOQ_Tree.FieldValues['pid'];
end;
procedure Tnew_tree.BtnSaveClick(Sender: TObject);
var
Sql_Txt:string;
begin
If EdtFid.Text='' then EdtFid.Text:='0';
If str='增加' Then
Try
Sql_Txt:='insert into product_type values('''+Edtid.Text
+''','''+Edtname.Text+''','''+EdtFid.Text+''')';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
Except
Application.MessageBox('记录增加失败!!!','提示信息',0);
end;
If str='修改' Then
Try
Sql_Txt:='update product_type set id='''+Edtid.Text
+''',name='''+Edtname.Text+''',pid='''+EdtFid.Text
+''' Where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
Except
Application.MessageBox('记录修改失败!!!','提示信息',0);
end;
Button3Click(nil);
end;
procedure Tnew_tree.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
PidString;
Sql_Txt:String;
begin
Pid:=Node.Data;
Sql_Txt:= 'select * from product_type where id='''+Pid^+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,True);
tempstr:=ADOQ_Tree.FieldByName('id').Text;
tempname:=ADOQ_Tree.FieldByName('name').Text;
end;
procedure Tnew_tree.BtnDeleteClick(Sender: TObject);
var
Sql_Txt:String;
begin
If MessageDlg('你确认删除该记录吗?',mtWarning,[mbYES,mbNO],0)=mrYES then
try
Sql_Txt:= 'delete from product_type where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
Init();
Except
Application.MessageBox('删除失败!!!','提示',0);
end;
end;
procedure Tnew_tree.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var
TargetNode,SourceNode:TTreeNode;
begin
TargetNode:=TreeView1.GetNodeAt(X,Y);
If (Source=Sender) and (TargetNode<>nil) then
begin
Accept:=True;
SourceNode:=TreeView1.Selected;
while (TargetNode.Parent<>nil) and (TargetNode <> SourceNode) do
TargetNode:=TargetNode.Parent;
if (TargetNode = SourceNode) then Accept:=False;
end
else Accept:=False;
end;
procedure Tnew_tree.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
targetnode,sourcenode:TTreenode;
Pstrstring;
Sql_Txt:String;
begin
TargetNode:=TreeView1.GetNodeAt(x,y);
SourceNode:=TreeView1.Selected;
Pstr:=TargetNode.Data;
Sql_Txt:= 'update product_type set pid='''+Pstr^+''' where id='''+tempstr+'''';
Exec_SQL(ADOQ_Tree,Sql_Txt,False);
TreeView1.Items.BeginUpdate;
try
CopyNodeUnder(TreeView1,SourceNode,TargetNode);
TreeView1.Selected:=TargetNode;
finally
TreeView1.Items.EndUpdate;
end;
end;
procedure Tnew_tree.CopyNodeUnder(TreeView:TTreeView;SourceNode,TargetNode:TtreeNode);
var
Node:TTreeNode;
i:integer;
begin
Node:=TreeView.Items.AddChildFirst(TargetNode,'');
Node.Assign(SourceNode);
for i:=SourceNode.Count-1 downto 0 do
begin
CopyNodeUnder(Treeview,SourceNode.Item,Node);
end;
TreeView.Items.Delete(SourceNode);
end;
procedure Tnew_tree.Init;
begin
ShowTree(TreeView1,ADOQ_Tree);
end;
procedure Tnew_tree.Button3Click(Sender: TObject);
begin
Init();
Clear_Edt;
end;
procedure Tnew_tree.BtnExitClick(Sender: TObject);
begin
Close;
end;
procedure Tnew_tree.Clear_Edt;
begin
EdtId.Text:='';
EdtName.Text:='';
EdtFid.Text:='';
str:='';
tempstr:='';
BtnEnabled();
end;
Function Tnew_tree.Exec_SQL(Que:TQuery;Text:String;Flag:Boolean=True):Boolean;
Begin
With Que Do
begin
Close;
Sql.Clear;
Sql.Add(Text);
If Flag Then
Open
Else
ExecSQL;
end;
End;
end.