ClientDataSet通过UpdateStatus方法配合Delta属性查找已删除的记录

 
  1. procedure TFrmTempSaleContract.Button1Click(Sender: TObject);
  2. var
  3.   TestXML:TNativeXml;
  4.   RootNode,MainNode,LineNode,TempNode:TXmlNode;
  5. begin
  6.   inherited;
  7.   try
  8.   TestXML:=TNativeXml.CreateName('Test');
  9.   TestXML.EncodingString:='GB2312';
  10.   RootNode:=TestXML.Root;
  11.   if CDSMain.ChangeCount>0 then
  12.     TestCdsMain.Data:=CDSMain.Delta
  13.   else
  14.     TestCdsMain.Data:=CDSMain.Data;
  15.   if CDSLine.ChangeCount>0 then
  16.     TestCdsLine.Data:=CDSLine.Delta
  17.   else
  18.     TestCdsLine.Data:=CDSLine.Data;
  19.   TestCdsMain.First;
  20.   while not TestCdsMain.Eof do
  21.   begin
  22.     If TestCdsMain.UpdateStatus<>usUnmodified then
  23.     begin
  24.       if TestCdsMain.UpdateStatus=usInserted then
  25.       begin
  26.         MainNode:=RootNode.NodeNew('Main');
  27.         MainNode.AttributeAdd('Job','New');
  28.         TestCdsLine.First;
  29.         while not TestCdsLine.Eof do
  30.         begin
  31.           LineNode:=MainNode.NodeNew('Line1');
  32.           LineNode.AttributeAdd('Job','New');
  33.           TestCdsLine.Next;
  34.         end;
  35.       end;
  36.       if TestCdsMain.UpdateStatus=usModified then
  37.       begin
  38.         MainNode:=RootNode.NodeNew('Main');
  39.         MainNode.AttributeAdd('Job','Update');
  40.         TestCdsLine.First;
  41.         while not TestCdsLine.Eof do
  42.         begin
  43.           LineNode:=MainNode.NodeNew('Line1');
  44.           LineNode.AttributeAdd('Job','Update');
  45.           TestCdsLine.Next;
  46.         end;
  47.       end;
  48.       if TestCdsMain.UpdateStatus=usDeleted then
  49.       begin
  50.         MainNode:=RootNode.NodeNew('Main');
  51.         MainNode.AttributeAdd('Job','Remove');
  52.         TestCdsLine.First;
  53.         while not TestCdsLine.Eof do
  54.         begin
  55.           LineNode:=MainNode.NodeNew('Line1');
  56.           LineNode.AttributeAdd('Job','Remove');
  57.           TestCdsLine.Next;
  58.         end;
  59.       end;
  60.     end
  61.     else
  62.     begin
  63.       MainNode:=RootNode.NodeNew('Main');
  64.       MainNode.AttributeAdd('Job','None');
  65.       TestCdsLine.First;
  66.       while not TestCdsLine.Eof do
  67.       begin
  68.         if TestCdsLine.UpdateStatus<>usUnmodified then
  69.         begin
  70.           LineNode:=MainNode.NodeNew('Line1');
  71.           if TestCdsLine.UpdateStatus=usModified then
  72.           begin
  73.             LineNode.AttributeAdd('Job','Update');  
  74.           end;
  75.           if TestCdsLine.UpdateStatus=usInserted then
  76.           begin
  77.             LineNode.AttributeAdd('Job','New');
  78.           end;
  79.           if TestCdsLine.UpdateStatus=usDeleted then
  80.           begin
  81.             LineNode.AttributeAdd('Job','Remove');
  82.           end;
  83.         end;
  84.         TestCdsLine.Next;
  85.       end;
  86.     end;
  87.     TestCdsMain.Next;
  88.   end;
  89.   ShowMessage(TestXML.WriteToString);
  90.   finally
  91.     TestXML.Free;
  92.   end;
  93.   
  94. end;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Delphi 6中,ClientDataSet的AddIndex方法用于手动添加索引。下面是使用AddIndex方法的示例代码: ``` // 假设有一个名为cds 的ClientDataSet对象 // 开始事务 cds.Active := True; cds.DisableControls; cds.IndexDefs.Update; try // 添加一个名为"IndexName"的索引 with cds.IndexDefs.AddIndexDef do begin Name := 'IndexName'; // 索引名称 Fields := 'FieldName1;FieldName2'; // 需要包含在索引中的字段名,多个字段之间使用分号分隔 Options := []; // 索引选项,可以根据需要设置选项,如Unique(唯一性)、Descending(降序)等 end; // 应用索引 cds.IndexName := 'IndexName'; // 打开数据集 cds.Open; // 在添加索引后,可以通过Locate或FindKey方法等进行定位和查找操作 finally // 结束事务 cds.EnableControls; end; ``` 上述代码首先启用ClientDataSet,然后禁用控件以提高效率。接着使用IndexDefs.AddIndexDef方法创建一个新的索引定义,并设置索引的名称、需要包含的字段以及其他选项。然后将创建的索引应用到ClientDataSet的IndexName属性中。最后打开数据集,并进行定位和查找操作。 需要注意的是,索引定义的添加和应用必须在数据集处于Active状态之前进行。另外,在使用AddIndex方法之前,建议先调用IndexDefs.Update方法更新索引定义,以确保索引定义与数据集的字段结构保持同步。 通过以上步骤,你可以在Delphi 6中使用ClientDataSet的AddIndex方法手动添加索引。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值