1 unit Unit2;
2
3 interface
4
5 uses
6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Data.Win.ADODB, Vcl.Grids,
8 Vcl.DBGrids, Vcl.ComCtrls, Vcl.StdCtrls;
9
10 type
11 TForm2 = class(TForm)
12 TreeView1: TTreeView;
13 DBGrid1: TDBGrid;
14 ADOConnection1: TADOConnection;
15 ADOQuery1: TADOQuery;
16 DataSource1: TDataSource;
17 procedure FormShow(Sender: TObject);
18 procedure TreeView1DblClick(Sender: TObject);
19 private
20 { Private declarations }
21 public
22 { Public declarations }
23 end;
24
25 var
26 Form2: TForm2;
27
28 implementation
29
30 {$R *.dfm}
31
32 procedure TForm2.FormShow(Sender: TObject);
33 var
34 I: Integer;
35 node: TTreeNode;
36 MyFuAdq,MyZiAdq: TADOQuery;
37 begin
38 MyFuAdq := TADOQuery.Create(nil);
39 MyZiAdq := TADOQuery.Create(nil);
40 try
41 MyFuAdq.Connection := ADOConnection1;
42 MyZiAdq.Connection := ADOConnection1;
43 with MyFuAdq do
44 begin
45 SQL.Text := 'SELECT TOP 1 * FROM top_order';
46 Open;
47 end;
48
49 for I := 0 to MyFuAdq.FieldDefs.Count - 1 do
50 begin
51 node := TreeView1.Items.Add(nil, MyFuAdq.FieldDefs[I].Name); {字段名}
52
53 //再次查询每列不同的数据
54 with MyZiAdq do
55 begin
56 Close;
57 SQL.Text := 'SELECT DISTINCT '+MyFuAdq.FieldDefs[I].Name+ ' FROM top_order';
58 Open;
59 end;
60 while not MyZiAdq.Eof do
61 begin
62 TreeView1.Items.AddChild(node, MyZiAdq.FieldByName(MyFuAdq.FieldDefs[I].Name).AsString);
63 MyZiAdq.Next;
64 end;
65 end;
66
67
68 finally
69 MyFuAdq.Free;
70 MyZiAdq.Free;
71 end;
72
73 end;
74
75 procedure TForm2.TreeView1DblClick(Sender: TObject);
76 var
77 MyAdq: TADOQuery;
78 selectName,selectValue: string;
79 begin
80 MyAdq := TADOQuery.Create(nil);
81 try
82 MyAdq.Connection := ADOConnection1;
83
84 //获取选中的内容
85 selectName := TreeView1.Selected.Parent.Text;
86 selectValue := TreeView1.Selected.Text;
87
88 //选中
89 with MyAdq do
90 begin
91 SQL.Text := 'UPDATE top_trade SET top_xuanzhong = 1 WHERE top_xuhao IN (SELECT DISTINCT top_xuhao FROM top_order WHERE '+selectName+' = '+selectValue.QuotedString+')';
92 ExecSQL;
93 end;
94
95
96 //查询
97 with ADOQuery1 do
98 begin
99 Close;
100 SQL.Text := 'SELECT top_order.top_title,top_trade.top_xuhao,top_trade.top_xuanzhong FROM top_order FULL JOIN top_trade ON top_order.top_xuhao = top_trade.top_xuhao WHERE top_order.'+selectName+' = '+selectValue.QuotedString;
101 Open;
102 end;
103 finally
104 MyAdq.Free;
105 end;
106 end;
107
108 end.
![复制代码]()
![]()