【问题标题】:Delphi - TClientDataSet: Second call to applyupdates() does not apply the updatesDelphi - TClientDataSet:对applyupdates()的第二次调用不应用更新
【发布时间】:2010-07-02 10:51:37
【问题描述】:

我再次遇到了 TClientDataSet 的问题。 我想这很简单,但我现在挣扎了一段时间。

这里有一些代码显示了我想要做什么:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ClientDataSet1.Insert;
  ClientDataSet1.FieldByName('anruf_von').AsDateTime := time;
  ClientDataSet1.Post;
  ClientDataSet1.ApplyUpdates(0); // without this applyUpdates in button2 works. 
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ClientDataSet1.edit;
  ClientDataSet1.FieldByName('anruf_bis').AsDateTime := time;
  ClientDataSet1.Post;
  showmessage(intToStr(ClientDataSet1.ChangeCount)); // returns 1
  if ClientDataSet1.ChangeCount > 0 then
    ClientDataSet1.applyUpdates(0);
end;

我认为代码是自我解释的。 当我按下 button1 时,会创建一条记录,并在调用 applyUpdates 后将其写入数据库。 当我按下按钮 2 时,我想更改此记录并将更新应用到数据库 - 这不起作用。 但是当我注释掉button1中的applyUpdates时,button2中的applyUpdates可以正常工作。

【问题讨论】:

  • 不起作用?你这是什么意思?
  • 我的意思是更新没有写入数据库表。
  • 在这种情况下,我会检查服务器端以查看提供程序使用了什么 SQL。或者第一次调用 (Insert) 没有返回主键字段的值(例如,如果它是由服务器自动生成的),这就是更新失败的原因?
  • 只是猜测:为了更新新插入的记录,需要先刷新ClientDataSet。您是否定义了 OnReconcileError 和 OnPostError 的事件处理程序?

标签: delphi tclientdataset


【解决方案1】:

尝试将Provider.UpdateMode改为upWhereKeyOnly,并在Provider.OnUpdateData中设置key字段。

我的猜测是插入总是有效的,因为它被执行为

 INSERT INTO ATABLE (anruf_von, anruf_bis) VALUES (...)

但更新失败,因为 WHERE 部分会将数据库存储的时间与来自客户端数据集的时间相匹配。 事实上,你可能会尝试匹配两个双打,这是不行的。

 UPDATE ATABLE SET anruf_bis=<Time> 
 WHERE anruf_von=<WRONG Time, with more precision than stored in db>

当你将 UpdateMode 设置为 upWhereKeyOnly 时,生成的 SQL 应该是这样的

 UPDATE ATABLE SET anruf_bis=<Time> 
 WHERE ID=<ID Value>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    相关资源
    最近更新 更多