【发布时间】:2019-05-23 18:48:08
【问题描述】:
我想知道为什么我的客户端数据集上发布的数据没有使用 ApplyUpdates 更新到 MySQL 数据库表。
我正在处理 2 个数据库。
DB1.CustomerTable1:没有问题,主键位于“ListID”—>CDS.Append —>CDS.ApplyUpdates
DB2.CustomerTable2:有问题,主键位于“Guid”—> CDS.Edit —> CDS.ApplyUpdates。
我在 DB2.CustomerTable2 上添加了带有“ListID”的主键,但仍然无法正常工作。
下面的代码,我正在使用。
procedure TfrmMain.spbExportClick(Sender: TObject);
var
Guid , VarAccountId, VarListSasId, VarListDspId : Variant;
Status : String;
begin
with dm.dmForm do
begin
cdsCustomer2.first;
while not cdsCustomer2.eof do
begin
//variable data for CDS.Customer1.AllFields
Guid := cdsCustomer2.FieldByName ('Guid').AsString;
VarAccountId := cdsCustomer2.FieldByName('ListID').AsString;
VarListSasId := cdsCustomer2.FieldByName('FullName').AsString;
VarListDspId := cdsCustomer2.FieldByName('Name').AsString;
Status := 'Out';
//posting to CDS.Customer1.AllFields
cdsCustomer1.DisableControls;
cdsCustomer1.Append;
cdsCustomer1.FieldByName('GUID').AsString := Guid;
cdsCustomer1.FieldByName('AccountId').AsString := VarAccountId;
cdsCustomer1.FieldByName('ListSasID').AsString := VarListSasId;
cdsCustomer1.FieldByName('ListDspID').AsString := VarListDspId;
cdsCustomer1.FieldByName('Status').AsString := Status;
cdsCustomer1.EnableControls;
cdsCustomer1.Fields[1].ProviderFlags := [pfInKey];
cdsCustomer1.Post;
//posting Guid value back to CDS.Customer2
if cdsCustomer2.locate('ListID', VarAccountId, []) then
begin
cdsCustomer2.DisableControls;
cdsCustomer2.Edit;
cdsCustomer2.FieldByName('ExternalGUID').AsString := Guid;
cdsCustomer2.EnableControls;
cdsCustomer2.Fields[0].ProviderFlags := [pfInKey];
cdsCustomer2.Post;
end;
cdsCustomer2.Next;
end;
//ApplyUpdates to mysql Customer1.Table and Customer2.Table
cdsCustomer1.ApplyUpdates(-1);
cdsCustomer2.ApplyUpdates(-1);
end;
end;
我希望这些代码会像数据库 1 一样简单地发布到我的 MySQL 数据库 2。除了发布 DB2 的 cdsCustomer2 之外,一切都可以发布到两个 ClientDataSet。
如果我在这里遗漏了一些您可能需要的信息,请告诉我。
附: 顺便说一下,这是数据库结构: UniConnection -> MySQLUniProvider -> UniQuery -> DataSetProvider -> ClientDataSet -> DataSource -> DBGrid
【问题讨论】:
-
cdsCustomer2.locate 调用看起来是多余的,因为键值是从之前的当前记录中获取的。此外,DisableControls 调用之前的注释看起来与实际代码无关。
-
@UweRaabe 谢谢。为混乱道歉。