Object Pascal:TClientDataset 删除
我正在使用 TClientDataset 创建内存数据集作为接收缓冲区。添加数据很棒,但是一旦我开始处理它,我希望能够从数据集中删除该行。调用删除有效 - 某种程度上 - 行/索引仍然可以访问,但现在不包含有效信息。
这让事情变得有点困难,因为当我处理这个缓冲区时,并不能保证条目实际上会被删除。我不想从第一个条目开始扫描缓冲区并跳过空项目,那么有没有更好的方法来从数据集中永久“删除”该项目?我的想法是它应该像实际的 SQL 表一样工作,其中删除行不会留下空记录。
实现这一目标的最佳方法是什么,或者我完全使用了错误的组件?
I'm creating an in-memory dataset using a TClientDataset to use as a receive buffer. Adding data is great, but once I go to process it I want to be able to remove the row from the dataset. Calling delete works - sort of - the row/index is still accessible but now contains no valid information.
This makes things a bit difficult since when I'm processing this buffer it's not guaranteed that entries will be deleted in fact. I'd rather not start scanning the buffer from the first entry and skipping empty items, so is there a better way to permanently "remove" the item from the dataset? My idea was that it should work something like an actual SQL table where deleting a row doesn't leave empty records.
What's the best way to achieve this, or am I using the wrong component entirely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,客户端数据集保留更改的“日志”,因为它们还被设计为能够将客户端更改发送到远程服务器,即使它们是在断开连接的会话中进行的(“公文包模型”)。通常,当您将更改应用于远程数据库时,此日志会被“清除”,并且任何其他更改都会与您的“本地”副本合并。
如果您不需要并希望直接进行更改,请将 LogChanges 设置为 False。
By default client datasets mantain a "log" of changes because they are also designed to be able to send client side changes to a remote server, even if they were made in a disconnected session ("briefcase model"). Usually this log is "cleared" when you apply the changes to the remote db, and any other changes is merged with your "local" copy.
Set LogChanges to False if you don't need it and wish that changes are made directly.
你的代码有问题。我为此案例准备了一个测试应用程序,因为几天后我将在多线程环境中面对TClientDataSet。我的测试用例应用程序没有出现此问题(Delphi 2010 Update 5),
几天后我也会在我自己的博客中发布此代码...现在我将其提供给您:
DFM 文件:
pas 文件:
I不会发布进一步的解释,因为您似乎精通多线程。如果您有任何疑问,请随时评论提问。
只有一件事...我打算使用 TMultiReadExclusiveWriteSynchronizer 来实现更好的并发性,但我没有将 ReadAccess 升级为 WriteAccess 的经验,因此我使用了 CriticalSection 来避免现在需要调查的时间。
There's something wrong with your code. I prepared a test application for this case, because I will face TClientDataSet in Multithreading environment in a few days. My test case application is not presenting this problem (Delphi 2010 Update 5)
I'll publish this code also in my own blog in a couple of days... for now I gave it to you now:
DFM file:
pas file:
I'll no post further explanation, because you seems well versed in multi-threading. If you have any doubt, feel free to comment with questions.
Only one thing... I was planning to use TMultiReadExclusiveWriteSynchronizer to allow better concurrence, but I have no experience in promoting ReadAccess to WriteAccess, so I used a CriticalSection to avoid the time needed to investigate right now.
关于您的代码的一些评论。
您正在使用一种不寻常的方式来循环数据集(使用计数器并仍然使用 next)。
删除时我的首选方向是从尾到头。
删除后您不会发布数据集。
我的建议是尝试这样的事情:
A couple of remarks regarding your code.
You are using an unusual way to loop through your dataset (using a counter and still using next).
My preferred direction when deleting would be from end to beginning.
You do not post your dataset after delete.
My suggestion would be to try something like this: