如何将所有 TClientDataSet 记录标记为已插入?
我有一个复杂的事务,它将来自多个 TClientDataSet 的数据保存在数据库中。
这些 ClientDataSets 之一总是将数据附加到底层表中,例如。生成 INSERT 语句,无论现有记录来自何处。
我现在强制插入:
// Create temp table, assign all target data,
// Empty target table, append data from temp
Tmp := TClientDataSet.Create;
Tmp.Data := Table.Data;
Table.MergeChangeLog;
Table.EmptyDataSet;
Tmp.First;
// Append all records
While not Tmp.Eof do
begin
Table.Append;
for i := 0 to Table.FieldCount - 1 do
Table.Fields[i].Value := Tmp.Fields[i].Value
Table.Post;
Tmp.Next;
end;
Tmp.Free;
是否有更简单的方法将所有记录标记为已插入?
I have a complex transaction that saves data from multiple TClientDataSets in database.
One of those ClientDataSets always append data to underlaying table, eg. generate INSERT statements, regardless of where existing records came from.
I'm forcing inserts right now with:
// Create temp table, assign all target data,
// Empty target table, append data from temp
Tmp := TClientDataSet.Create;
Tmp.Data := Table.Data;
Table.MergeChangeLog;
Table.EmptyDataSet;
Tmp.First;
// Append all records
While not Tmp.Eof do
begin
Table.Append;
for i := 0 to Table.FieldCount - 1 do
Table.Fields[i].Value := Tmp.Fields[i].Value
Table.Post;
Tmp.Next;
end;
Tmp.Free;
Is there a simpler way to just mark all records as inserted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
人们会希望某事。像这样会起作用(至少当没有计算字段时);
嗯,它可以工作,但只要没有重新检索数据即可。也就是说,
TCustomClientDataSet.GetRecord
重新设置记录属性。我想这使得黑客方法变得毫无用处。也许可以尝试绑定一个数据感知控件,看看设置数据源的数据链接的 BufferCount 是否有帮助。或者,尝试重写后代 ClientDataSet 中的 GetRecord。但我怀疑这是否值得付出努力。
[编辑]
记录属性可以在“AfterScroll”事件中被破解。例如,这将有助于代码测试 UpdateStatus 以返回“usInserted”。
测试是否所有记录都插入
A new TClientDataSet can be derived to retrieve always "inserted" records.
(类型声明位于包含 ClientDataSet 的表单/数据模块之前)
现在,对于所有记录,UpdateStatus 将返回“usInserted”。
编辑
我终于明白了,目的是为所有记录生成插入SQL。我们不会通过修改 DataSet 的记录属性来实现这一点,ApplyUpdates 仅考虑“Delta”,并且我们可能没有 Delta。可能有不同的方法来实现这一点,下面的示例假设 DataSet 有一个 Provider,并且我们能够在其上放置一个事件处理程序。
One would hope sth. like this would work (at least when there are no calculated fields);
Well, it works, but as long as no data is re-retrieved. That is,
TCustomClientDataSet.GetRecord
re-sets the record attributes. This renders the hacky approach pretty useless I guess.Maybe one could try binding a data-aware control and see if setting the BufferCount of the DataLink of the DataSource would help. Or, try overriding GetRecord in a descendant ClientDataSet. But I doubt it is worth the effort.
[EDIT]
The record attribute can be hacked in an "AfterScroll" event. This would help, for instance, code testing the UpdateStatus to return "usInserted".
Test if all the records are inserted
A new TClientDataSet can be derived to retrieve always "inserted" records.
(The type declaration goes before the form/datamoule containing the ClientDataSet)
Now for all records, UpdateStatus will return "usInserted".
EDIT
I finally understood that the aim is to generate insert SQLs for all records. We won't achieve this by modifying records' attributes of the DataSet, ApplyUpdates takes only the "Delta" into consideration and we possibly have no Delta. There might be different ways to achieve this, the below example assumes the DataSet has a Provider and we are able to put an event handler on it.