2007. április 30., hétfő
How to copy records to the same table
Problem/Question/Abstract:
I need to copy a record in a dBase table to the same table and just change a value or two. I know that I can copy the hard way read all the fields into a record then write it back out.
Answer:
Solve 1:
var
SourceQueryFieldName: string;
begin
QueryDestination.Open;
QuerySource.Open;
QueryDestination.Insert;
for FieldLoop := 0 to QuerySource.FieldCount - 1 do
begin
SourceQueryFieldName := DataBaseQuerySource.Fields[FieldLoop].FieldName;
try
QueryDestination[SourceQueryFieldName] := QuerySource[SourceQueryFieldName];
except
{Field not Found}
end;
end;
QueryDestination.Post;
QueryDestination.Close;
QuerySource.Close;
end;
Solve 2:
I actually prefer code that reads each field and writes it to the new record like this:
procedure CopyRecord(tbl: TTable);
var
I: Integer;
tblTmp: TTable;
begin
blTmp := TTable.Create(nil);
try
tblTmp.DatabaseName := tbl.DatabaseName;
tblTmp.TableName := tbl.TableName;
tblTmp.Open;
ttblTmp.GotoCursor(Src);
tbl.Insert;
try
for I := 0 to T.FieldCount - 1 do
tbl.Fields[I].Assign(tblTmp.Fields[I]);
except
tbl.Cancel;
raise;
end;
finally
tblTmp.Free;
end;
end;
But you can also do it like this:
procedure CopyRecord(const FromTable: TTable);
begin
dbiInsertRecord(FromTable.Handle, dbiNoLock, FromTable.ActiveBuffer);
end;
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése