Forum > Databases
get record numer from dataset
(1/1)
xaver:
Hi,
I'm desperately trying to get the entire record Nr. from a data set:
function TForm1.DataRowToString(const aDataset: TDataSet; aRecNo: Integer): String;
var
i: Integer;
begin
aDataset.RecNo := aRecNo;
Result := '';
for i := 0 to aDataset.FieldCount - 1 do
Result := Result + aDataset.Fields.AsString + ' ';
Result := Trim(Result);
end;
procedure TForm1.Button9Click(Sender: TObject);
var i :Integer;
row : String;
begin
for i:=0 to DataSource1.DataSet.RecordCount -1 do
row:= DataRowToString(DataSource1.DataSet, i);
Memo1.Lines.Add(row);
end;
Unfortunately I'm receiving the following error message:
CSVDataset1 : Could not find the requested record.
Could someone help explaining with an example how to get a specific record by index ?
Thanks.
Xaver.
dsiders:
--- Quote from: xaver on May 01, 2021, 03:20:40 am ---Hi,
I'm desperately trying to get the entire record Nr. from a data set:
function TForm1.DataRowToString(const aDataset: TDataSet; aRecNo: Integer): String;
var
i: Integer;
begin
aDataset.RecNo := aRecNo;
Result := '';
for i := 0 to aDataset.FieldCount - 1 do
Result := Result + aDataset.Fields.AsString + ' ';
Result := Trim(Result);
end;
procedure TForm1.Button9Click(Sender: TObject);
var i :Integer;
row : String;
begin
for i:=0 to DataSource1.DataSet.RecordCount -1 do
row:= DataRowToString(DataSource1.DataSet, i);
Memo1.Lines.Add(row);
end;
Unfortunately I'm receiving the following error message:
CSVDataset1 : Could not find the requested record.
Could someone help explaining with an example how to get a specific record by index ?
Thanks.
Xaver.
--- End quote ---
Your loop iterates from 0 to RecCount-1 and valid record numbers are in the range 1..RecCount.
egsuh:
You should not try to use RecNo. Please refer to the following page.
https://lazarus-ccr.sourceforge.io/docs/fcl/db/tdataset.recno.html
It says, first, recno property must be implemented by TDataSet's descendants. Secondly, it says not to use it because it is not reliable.
BTW, dataset is a set, not ordered list. You'd better implement some other method to identify a specific record.
Zvoni:
What the others said about RecNo not withstanding:
You seem to try to concatenate all Fields to a single string.
Why not read all fields into an array, and then send the array to a Join?
https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.join.html
Note: Any Fields, which are "empty" you have to handle yourself (a.k.a include/exclude from the result)
EDIT:
--- Quote ---BTW, dataset is a set, not ordered list. You'd better implement some other method to identify a specific record.
--- End quote ---
Nevermind, that the source for it is probably a Query.
This bears the question: Why not concat it within the SQL-Statement?
Which brings us to the next question: Which DBMS?
Navigation
[0] Message Index