简体   繁体   English

使用数据库查询中的列填充TListBox

[英]Filling a TListBox with a column from a database query

I created a table in my database with MYDAC components and added data. 我在数据库中使用MYDAC组件创建了一个表并添加了数据。 How can i extract these values that in colums from this table? 如何从此表中提取这些值?

i tried these but they didn't work; 我尝试了这些,但是它们没有用;

 MyQuery1.Close;
 MyQuery1.SQL.Text :='SELECT * FROM uyeler ORDER BY site';
 Listbox1.Items.Add(MyQuery1.ParamByName('site').AsString);
 MyQuery1.Execute; 

uyeler = table uyeler =表格

site = colums 站点=列

Editors Note: Based on comments made to accepted answer, this question is asking how to populate a TListBox with the data returned in a column of a query. 编者注:根据对已接受答案的评论,此问题询问如何用查询列中返回的数据填充TListBox

Your question is a little unclear. 您的问题还不清楚。 You ask how to add column data to a TListBox , but your code sample shows something about ParamByName , which looks like you're trying to assign a ListBox value to a SQL parameter. 你问如何列数据添加到TListBox ,但你的代码示例显示了一些关于ParamByName ,它看起来像你想指定一个ListBox值的SQL参数。

Note : Question clarified based on comment to this reply. 注意 :根据对此回复的评论,问题得到了澄清。 The first code answers the question; 第一个代码回答了这个问题; I'll leave the second as an example for future readers because it's already written. 我将第二个示例留给以后的读者使用,因为它已经写好了。

If you're trying to fill a TListBox from a database column, you need to run the query, and then loop through the data to add the column data to your TListBox . 如果尝试从数据库列填充TListBox ,则需要运行查询,然后遍历数据以将列数据添加到TListBox (You also shouldn't SELECT * when you're only going to use the site column; it means extra data is being sent from the database to the application when the data will never be used and will end up being thrown away.) (当您仅要使用site列时,也不应SELECT * ;这意味着多余的数据将从数据库发送到应用程序,而这些数据将永远不会被使用并最终被丢弃。)

This should help you get started: 这应该可以帮助您入门:

MyQuery1.Close;
MyQuery1.SQL.Text := 'SELECT site FROM uyeler ORDER BY site';
try
  MyQuery1.Open;
  ListBox1.Items.Clear;
  while not MyQuery1.Eof do
  begin
    ListBox1.Items.Add(MyQuery1.Fields[0].AsString);
    MyQuery1.Next;
  end;
finally
  MyQuery1.Close;
end;

If you're trying to do the second (populate a parameter with a value from a TListBox ), this should help: 如果您要进行第二次操作(使用TListBox的值填充参数),这应该会有所帮助:

// to shorten typing of long lines
var
  Site: string;
begin
  if ListBox1.ItemIndex <> -1 then
  begin
    MyQuery1.Close;
    MyQuery1.SQL.Clear;
    MyQuery1.SQL.Add('SELECT Column1, Column2, site FROM uyeler');
    MyQuery1.SQL.Add('WHERE site = :siteval ORDER BY site');
    Site := ListBox1.Items[ListBox1.ItemIndex];
    MyQuery1.ParamByName('siteval').AsString := Site;
    MyQuery1.Open;
    try
      // Use the database rows here
    finally
      MyQuery1.Close;
    end;
  end;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM