gpt4 book ai didi

database - 来自数据库列的 Tlistbox 项目

转载 作者:搜寻专家 更新时间:2023-10-30 21:52:29 24 4
gpt4 key购买 nike

有没有办法用数据库列中的项目填充 TListbox 控件?

我知道正确的方法是简单地使用 DBLookupListbox 控件并将其设置为访问我想要的列,但问题是当我单击其中一个项目时在查找控件中,它更改了数据集中的当前行(预期行为),但我不希望这种情况发生。

相反,我希望仅在查找控件中双击事件时更改当前行,并且由于我不认为可以更改此行为,所以我认为简单地使用普通 TListBox 而不是,但正如我上面所说,我不确定它是如何完成的。

因此,我又一次向专家请教了一些关于如何使用数据库列中的项目填充普通 Tlistbox 控件的建议。

最佳答案

您没有指定 DB 您正在使用的组件,所以我使用 ADO 和 MySQL 编写了这个示例。

const
StrConnection='Driver={MySQL ODBC 5.1 Driver};Server=%s;Database=%s;User=%s; Password=%s;Option=3;';

procedure LoadColumn(Items:TStrings; const SqlStr :string);
Var
AdoDataSet : TADODataSet;
begin
AdoDataSet:=TADODataSet.Create(nil);
try
//you can share the connection too, in this case a new connection is made
AdoDataSet.ConnectionString:=Format(StrConnection,['server','mydatabase','user','pass']);;
AdoDataSet.CommandText:=SqlStr;
AdoDataSet.Open;
if not AdoDataSet.IsEmpty then
begin
Items.BeginUpdate;
try
Items.Clear;
while not AdoDataSet.Eof do
begin
Items.Add(AdoDataSet.Fields[0].AsString);
AdoDataSet.Next;
end;
finally
Items.EndUpdate;
end;
end;
finally
AdoDataSet.Free;
end;
end;

然后像这样使用

   LoadColumn(ListBox1.Items, 'Select MyColumn FROM Table');

关于database - 来自数据库列的 Tlistbox 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11551099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com