[英]Copy record from one Table to another not working
I am using the BDE and flat Tables. 我正在使用BDE和平面表。 I have two identical Tables,
tblOne
and tblTwo
I am trying to copy the data from one table to the other. 我有两个相同的表,
tblOne
和tblTwo
我正在尝试将数据从一个表复制到另一个表。 Not the entire DB, just one specific Record using this: 不是整个数据库,而是一个使用此的特定记录:
function Tdm.CopyRecord(var tblFrom,tblTo : TTable) : Boolean;
var
i : Integer;
begin
Result:=False;
try
tblTo.Insert;
for i:=1 to tblFrom.FieldCount-1 do
begin
if tblFrom.Fields[i].FieldName = tblTo.Fields[i].FieldName then
tblTo.Fields[i].Value:=tblFrom.Fields[i].Value;
end;
tblTo.Post;
Result:=True;
finally
end;
end;
if CopyRecord(tblOne,tblTwo) then...
Stepping through this all of the Values are "Null" for the From Table. 逐步执行此操作,对于“ From”表,所有值均为“ Null”。
After the Post I get a blank record added to the tblTo
. 发布后,我得到一个空白记录添加到
tblTo
。 Not surprising with all the Values a Null. 所有值都为零的毫不奇怪。 :)
:)
Where am I going wrong in copying the data? 复制数据时哪里出问题了? It is not making it to the Copy function.
它不能用于复制功能。
I have been at this for several hours and cannot make it work. 我在这里待了几个小时,无法使其正常工作。 Probably something simple I am over-looking.
我可能忽略了一些简单的事情。 I added the "var" parameter to see if that made any difference but it did not.
我添加了“ var”参数,以查看是否有什么不同,但没有。
Oh, by the by, I am starting the loop from "1" not "0" as the first field in both files is an AutoInc
. 哦,
AutoInc
,我正在从“ 1”而不是“ 0”开始循环,因为两个文件中的第一个字段都是AutoInc
。
Here is how I would do it: 这是我的方法:
function CopyRecord(tblFrom, tblTo: TTable; const StartIndex: Integer=0): Boolean;
var
i: Integer;
FieldFrom, FieldTo: TField;
begin
Result := False;
for i := StartIndex to tblFrom.FieldCount - 1 do
begin
FieldFrom := tblFrom.Fields[i];
FieldTo := tblTo.FindField(FieldFrom.FieldName);
if Assigned(FieldTo) then
begin
FieldTo.Value := FieldFrom.Value;
Result := True;
end;
end;
end;
I would not use tblTo.Insert
/ tblTo.Post
inside the CopyRecord
method. 我不会用
tblTo.Insert
/ tblTo.Post
里面 CopyRecord
方法。 but rather use it outside eg: 而是在外部使用它,例如:
tblTwo.Append;
if CopyRecord(tblOne, tblTwo, 1) then
tblTwo.Post
else
tblTwo.Cancel;
This could be re-used also in Edit
mode. 也可以在“
Edit
模式下重新使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.