I created a table in my database with MYDAC components and added data. 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
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.
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.
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
. (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.)
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:
// 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;