如何将treeview中的内容存入一个数据库中去?(高手请进) (200分)

  • 主题发起人 主题发起人 chemstar
  • 开始时间 开始时间
C

chemstar

Unregistered / Unconfirmed
GUEST, unregistred user!
我想做这样一个东西,实现以下功能:
1、用treeview显示C:/下面的文件名称,然后将其内容一条一条存入一个
数据库中去(Access文件)。
2、相反也可以进行:从数据库中读出所有文件名称,然后按原样显示在treeview中。
3、存入后还应方便进行一些编辑操作,比如:添加、删除、移动一些节点时也能及时
对数据库更新。

介绍现成的控件也成。
 
DevExpress DBTree 可以实现
 
這辦法不錯,可以寫成控件。
我最近實現了你的想法,不過沒有通用性。
要看你的數據庫結構是什麼樣了,思路就會不同的。
你他細說說你的需求吧,對應的表結構是什麼樣。
 
1stclass也有DBTreeView
下面的代码是使用TreeView手工操作数据库的,供参考:
里面有些字符是台湾繁体的。
=======================================================
unit uSAL01_F01;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ImgList, StdCtrls, Buttons, ExtCtrls, DBCtrls, ComCtrls, Db,
DBTables, Menus;

type TNodeType=(nsEdit,nsInsert,nsNormal);

type
TSAL01_F01 = class(TForm)
BitBtn1: TBitBtn;
ImageList1: TImageList;
PopupMenu1: TPopupMenu;
mnuRN: TMenuItem;
mnuNew: TMenuItem;
mnuDel: TMenuItem;
Query1: TQuery;
btnRN: TBitBtn;
btnNew: TBitBtn;
btnDel: TBitBtn;
Panel1: TPanel;
tvwCode: TTreeView;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure tvwCodeGetImageIndex(Sender: TObject; Node: TTreeNode);
procedure tvwCodeGetSelectedIndex(Sender: TObject; Node: TTreeNode);
procedure tvwCodeExpanded(Sender: TObject; Node: TTreeNode);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure tvwCodeClick(Sender: TObject);
procedure mnuRNClick(Sender: TObject);
procedure tvwCodeEdited(Sender: TObject; Node: TTreeNode;
var S: String);
procedure mnuNewClick(Sender: TObject);
procedure PopupMenu1Popup(Sender: TObject);
procedure mnuDelClick(Sender: TObject);
private
{ Private declarations }
NodeState : TNodeType;
Procedure LoadCode(crTbl:TDBDataSet);
public
{ Public declarations }
end;

var
SAL01_F01: TSAL01_F01;

CONST SFirstNodeTxt='初始结点';

implementation

uses uDM_SAL01_D01;

{$R *.DFM}

Procedure TSAL01_F01.LoadCode(crTbl:TDBDataSet);
var ID1,ID2:String;
Level:Integer;
MyNode:array[0..2]of TTreeNode;
begin
Screen.Cursor:=crHourGlass;
Level:=0;
With crTbl do
begin
if not Active then Open;
First;
tvwCode.Items.Clear;
MyNode[Level]:=tvwCode.Items.Add(tvwCode.TopItem,SFirstNodeTxt);
While Not Eof do
begin
if ID1 <> FieldByName('SLN_NO02').AsString then Level := 1
else Level := 2;
ID1 := FieldByName('SLN_NO02').AsString;
ID2 := FieldByName('SLN_NO03').AsString;
case Level of
1 : begin
MyNode[Level]:=tvwCode.Items.AddChild(MyNode[Level-1],ID1+'.'+FieldByName('SLN_NM01').AsString);
MyNode[Level+1]:=tvwCode.Items.AddChild(MyNode[Level],ID2+'.'+FieldByName('SLN_0001').AsString);
end;
2 : MyNode[Level]:=tvwCode.Items.AddChild(MyNode[Level-1],ID2+'.'+FieldByName('SLN_0001').AsString);
end;
Next;
end;
end;
MyNode[0].Expand(False);
Screen.Cursor:=crDefault;
end;

procedure TSAL01_F01.FormCreate(Sender: TObject);
begin
Left:=0; Top:=0;
Width:=405; Height:=487;
DM_SAL01_D01.T_SAL01_D01.Open;
LoadCode(DM_SAL01_D01.T_SAL01_D01);
NodeState:=nsNormal;
end;

procedure TSAL01_F01.tvwCodeGetImageIndex(Sender: TObject;
Node: TTreeNode);
begin
if Node.HasChildren then
if Node.Expanded then
Node.ImageIndex := 1
else
Node.ImageIndex := 0
else
Node.ImageIndex := 2;
end;

procedure TSAL01_F01.tvwCodeGetSelectedIndex(Sender: TObject;
Node: TTreeNode);
begin
Node.SelectedIndex := Node.ImageIndex;
end;

procedure TSAL01_F01.tvwCodeExpanded(Sender: TObject; Node: TTreeNode);
begin
tvwCode.Repaint;
end;

procedure TSAL01_F01.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;

procedure TSAL01_F01.tvwCodeClick(Sender: TObject);
begin
if tvwCode.Selected.Level=2 then
DM_SAL01_D01.T_SAL01_D01.Locate('SLN_0001;SLN_NM01',
VarArrayof([Copy(tvwCode.Selected.Text,Pos('.',tvwCode.Selected.Text)+1,MAXINT),
Copy(tvwCode.Selected.Parent.Text,Pos('.',tvwCode.Selected.Parent.Text)+1,MAXINT)]),[]);
btnRN.Enabled:=(tvwCode.Selected.Parent<>Nil);
btnDel.Enabled:=(tvwCode.Selected.Parent<>Nil);
btnNew.Enabled:=(tvwCode.Selected.Level<2);
end;

procedure TSAL01_F01.mnuRNClick(Sender: TObject);
begin
tvwCode.ReadOnly:=False;
tvwCode.Selected.EditText;
NodeState:=nsEdit;
end;

procedure TSAL01_F01.tvwCodeEdited(Sender: TObject; Node: TTreeNode;
var S: String);
Var
Operater : String;
begin
if NodeState=nsEdit then
begin
Case Node.Level of
1 : if Application.MessageBox('絋龟璶?э硂??竊翴盾?','牡?',MB_YESNO+MB_ICONWARNING)=IDYES then
With Query1 do
begin
Close;
SQL.Clear;
SQL.Add('UPDATE sal01_d01 SET SLN_NM01="'+Copy(S,Pos('.',S)+1,MAXINT)+'" WHERE SLN_NO02="'+Copy(Node.Text,Pos('.',Node.Text)+1,MAXINT)+'"');
TRY
EXECSQL;
EXCEPT
Raise Exception.Create('?э竊翴ア毖!');
END;
end;
2 : if DM_SAL01_D01.T_SAL01_D01.Locate('SLN_0001;SLN_NM01',
VarArrayof([Copy(Node.Text,Pos('.',tvwCode.Selected.Text)+1,MAXINT),
Copy(Node.Parent.Text,Pos('.',tvwCode.Selected.Parent.Text)+1,MAXINT)]),[]) then
begin
DM_SAL01_D01.T_SAL01_D01.Edit;
DM_SAL01_D01.T_SAL01_D01.FieldByName('SLN_0001').AsString:=Copy(S,Pos('.',S)+1,MAXINT);
DM_SAL01_D01.T_SAL01_D01.Post;
end;
end;
end;
if (NodeState=nsInsert)AND(Node.Level=2) then
begin
if StrtoInt(Copy(Node.Parent.Text,4,2)) > 50 then Operater:='-'
else Operater:='+';
TRY
DM_SAL01_D01.T_SAL01_D01.AppendRecord([
Copy(Node.Parent.Text,1,Pos('.',Node.Parent.Text)-1),
Copy(S,1,Pos('.',S)-1),Nil,
Copy(Node.Parent.Text,Pos('.',Node.Parent.Text)+1,MAXINT),
Copy(S,Pos('.',S)+1,MAXINT),Operater,'']);
EXCEPT
Raise Exception.Create('穝糤竊翴祇ネ岿粇!');
END;
end;
NodeState:=nsNormal;
tvwCode.ReadOnly:=True;
end;

procedure TSAL01_F01.mnuNewClick(Sender: TObject);
Var
NewNode : TTreeNode;
ShowTxt, NumStr: String;
begin
NodeState:=nsInsert;
Case tvwCode.Selected.Level of
0: With Query1 do
begin
Close;
SQL.Clear;
SQL.Add('SELECT MAX(SLN_NO02) FROM sal01_d01');
Open;
TRY
if Fields[0].AsString='' then NumStr:='01'
else NumStr:=FormatFloat('00',Strtoint(Copy(Fields[0].AsString,4,2))+1);
EXCEPT
Raise Exception.Create('岿粇?㏑?砏玥!');
END;
if Fields[0].AsString='' then ShowTxt :='SNL01.New contents'
else ShowTxt :=Copy(Fields[0].AsString,1,3)+NumStr+'.New contents';
end;
1,2: With Query1 do
begin
Close;
SQL.Clear;
SQL.Add('SELECT MAX(SLN_NO03) FROM sal01_d01 WHERE SLN_NO02="'+Copy(tvwCode.Selected.Text,1,Pos('.',tvwCode.Selected.Text)-1)+'"');
Open;
TRY
if Fields[0].AsString='' then NumStr:='01'
else NumStr:=FormatFloat('00',Strtoint(Fields[0].AsString)+1);
EXCEPT
Raise Exception.Create('岿粇?㏑?砏玥!');
END;
ShowTxt := NumStr+'.New contents';
end;
end;
if tvwCode.Selected.Level=2 then
NewNode:=tvwCode.Items.AddChild(tvwCode.Selected.Parent,ShowTxt)
else
NewNode:=tvwCode.Items.AddChild(tvwCode.Selected,ShowTxt);
tvwCode.Selected:=NewNode;
tvwCode.ReadOnly:=False;
NewNode.EditText;
end;

procedure TSAL01_F01.PopupMenu1Popup(Sender: TObject);
begin
mnuRN.Enabled:=(tvwCode.Selected.Parent<>Nil);
mnuDel.Enabled:=(tvwCode.Selected.Parent<>Nil);
mnuNew.Enabled:=(tvwCode.Selected.Level<2);
end;

procedure TSAL01_F01.mnuDelClick(Sender: TObject);
begin
if tvwCode.Selected.HasChildren then
begin
if Application.MessageBox('璶?埃硂?竊翴のㄤ???竊翴盾?','?埃牡?',MB_YESNO+MB_ICONWARNING)=IDYES then
begin
With Query1 do
begin
Close;
SQL.Clear;
if tvwCode.Selected.Level=1 then
SQL.Add('DELETE FROM sal01_d01 WHERE SLN_NM01="'+tvwCode.Selected.Text+'"');
TRY
EXECSQL;
EXCEPT
Raise Exception.Create('?埃竊翴?岿!');
END;
end;
tvwCode.Selected.DeleteChildren;
tvwCode.Selected.Delete;
end;
end
else
if Application.MessageBox('璶?埃硂?竊翴盾?','?埃牡?',MB_YESNO+MB_ICONWARNING)=IDYES then
begin
TRY
if DM_SAL01_D01.T_SAL01_D01.Locate('SLN_0001;SLN_NM01',
VarArrayof([Copy(tvwCode.Selected.Text,Pos('.',tvwCode.Selected.Text)+1,MAXINT),
Copy(tvwCode.Selected.Parent.Text,Pos('.',tvwCode.Selected.Parent.Text)+1,MAXINT)]),[])
then
DM_SAL01_D01.T_SAL01_D01.Delete;
EXCEPT
END;
tvwCode.Selected.Delete;
end;
end;

end.
 
to fxjpost:
treeview中树形结构存的就C:/下的文件夹名称,数据库中内容就是选中treeview中一项时
对应的文件夹的名称、大小、日期等信息。其实我就是想做一个类似windwos的资源管理器式
的东西。
 
有何特点?做出来干什么呢?
你的问题主要是TREEVIEW和数据对应关系的问题,在读取和写入时叫TREEVIEW记住一个表的唯一标识就行了,这种问题的解决方法论坛上太多,你仔细找找吧,多的你都看不完
 
这个问题我已经基本解决了,但是存诸到数据库时速度太慢了。大家知道是什么原因吗?
 
后退
顶部