delphi中ADOConnection1 和 ADOQuery1 控件怎么设置数据?
求编个对应的数据和设置方法:
以下代码,一句报错的也没有,就差设置数据库了,但我不会用,不懂咋搞,从未用过 这两控件。{:301_974:}
unit Unit5;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, vcl.fccombo, vcl.fcTreeCombo, Data.DB, Data.Win.ADODB,
Vcl.ComCtrls;
type
PNodeInfo = ^TNodeInfo;
TNodeInfo = record
ID: string;
FullName: string;
Url: string;
end;
TForm5 = class(TForm)
fcTreeCombo1: TfcTreeCombo;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
TreeView1: TTreeView;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure CreateChildTree(ParentNode: TTreeNode);
procedure CreateViewTree(Sender: TObject);
procedure TreeView1DblClick(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
end;
var
Form5: TForm5;
implementation
{$R *.dfm}
procedure TForm5.CreateChildTree(ParentNode: TTreeNode);
var
Query: TADOQuery;
ChildNode: TTreeNode; //孩子节点
ChildNodeInfo: PNodeInfo; //孩子节点信息
begin
Query := TADOQuery.Create(nil);
with Query do
begin
Connection := ADOConnection1;
SQL.Add('SELECT ID,FullName,Url FROM PInfo WHERE ParentID = ''' + PNodeInfo(ParentNode.Data)^.ID + ''''); //获取孩子节点信息
Open;
while not Eof do
begin
New(ChildNodeInfo);
ChildNodeInfo^.ID := FieldByName('ID').AsString;
ChildNodeInfo^.FullName := FieldByName('FullName').AsString;
ChildNodeInfo^.Url := FieldByName('Url').AsString;
ChildNode := TreeView1.Items.AddChildObject(ParentNode, (ChildNodeInfo^.FullName), ChildNodeInfo); //添加孩子节点,并关联孩子节点信息
CreateChildTree(ChildNode); //进行递归
Next;
end;
Close;
end;
end;
procedure TForm5.CreateViewTree(Sender: TObject);
var
BootNode: TTreeNode; //根节点
BootNodeInfo: PNodeInfo; //根节点信息
begin
with ADOQuery1 do
begin
SQL.Clear;
SQL.Add('SELECT ID,FullName,Url FROM PInfo WHERE ParentID IS NULL'); //获取根节点信息
Open;
New(BootNodeInfo);
BootNodeInfo^.ID := FieldByName('ID').AsString;
BootNodeInfo^.FullName := FieldByName('FullName').AsString;
BootNodeInfo^.Url := FieldByName('Url').AsString;
TreeView1.Items.Clear;
BootNode := TreeView1.Items.AddChildObject(nil, (BootNodeInfo^.FullName), BootNodeInfo); //添加根节点,并关联根节点信息
Close;
end;
CreateChildTree(BootNode); //创建子树
TreeView1.FullExpand; //展开所有树节点
end;
procedure TForm5.TreeView1DblClick(Sender: TObject);
var
TNode: TTreeNode;
X, Y: Integer;
begin
//获取双击的节点
with TreeView1 do
begin
X := ScreenToClient(Mouse.CursorPos).X;
Y := ScreenToClient(Mouse.CursorPos).Y;
TNode := GetNodeAt(X, Y);
end;
if (TNode <> nil) and (TNode = TreeView1.Selected) then
ShowMessage(PNodeInfo(TNode.Data)^.Url); //显示双击节点的信息
end;
procedure TForm5.Button1Click(Sender: TObject);
begin
CreateViewTree(Sender);
end;
end.
https://www.cnblogs.com/michellexiaoqi/p/7008165.html 你的 ADOConnection1 还没设置连接到何种数据库上. 风铃夜思雨 发表于 2022-9-15 00:46
你的 ADOConnection1 还没设置连接到何种数据库上.
对啊,不会才问啊。 去盒子问吧,大佬 这问题问的太初级了,找本书看看就会了 最简单就是双击ADOConnection1进行设置,要不就用连接数据库字符串进行设置 为还在坚守DELPHI的开发者点赞!{:1_921:}(曾经我也是DELPHI的狂热追随者) 7L 正解 参考2L 连接
TADOConnection
TADOQuery
在TADOConnection 中设置连接字符串 如sqlserver/mdb
然后TADOQuery关联上TADOConnection
就可以用了 pzx521521 发表于 2022-9-15 14:15
7L 正解 参考2L 连接
TADOConnection
TADOQuery
@pzx521521
创建Access*.mdb 说真的,这还真的不会,没玩过!
这种格式最简单吗?
需要设置服务器密码啥的吗?
百度出一堆,没用过,很晕头。
页:
[1]