• 一个ClientDataset的Delta与XML相互转换


    一个ClientDataset的Delta与XML相互转换的文章:


    大家都知道TClientDataSet的Delta属性保存数据集的变化,但是Delta是OleVariant类型的属性,这样如果用Delphi写Webservice,就有需要把它转成XML,特别是其他语言也要用这个Webservice的时候,因为如果Delta能和XML相互转换,Delta包的结构就是透明的了。而TClientDataSet却没有提供DeltaXML这样的属性,之前在网上找了一下,发现网上的都是把Delta赋给另一个TClientDataSet的Data,然后再通过XMLData返回XML,但这样为了一个简单的功能就得多创建一个对象实例,感觉很不好,于是研究一下TClientDataSet的源码,终于完成了Delta与XML相互转换的函数,代码如下:

    unit uDelta;

    interface

    uses DBClient,DSIntf,VarUtils,ActiveX;

    function DeltaToXML(Delta:OleVariant):String;
    function XMLToDelta(XMLData:String):OleVariant;

    implementation

    procedure Check(FDSBase:IDSBase;Status: DBResult);
    var
    ErrMsg: array[0..2048] of Char;
    begin
    if Status <> 0 then
    begin
        FDSBase.GetErrorString(Status, ErrMsg);
        raise EDBClient.Create(ErrMsg, Status);
    end;
    end;

    function DeltaToXML(Delta:OleVariant):String;
    var
    FDSBase: IDSBase;
    DataPacket: TDataPacket;
    VarPacket: OleVariant;
    begin
    CreateDbClientObject(CLSID_DSBase, IDSBase, FDSBase);
    SafeArrayCheck(ActiveX.SafeArrayCopy(VarToDataPacket(Delta), DataPacket));
    Check(FDSBase,FDSBase.AppendData(DataPacket, True));
    FDSBase.SetProp(dspropXML_STREAMMODE, xmlON);
    Check(FDSBase,FDSBase.StreamDS(DataPacket));
    DataPacketToVariant(DataPacket, VarPacket);
    Result := VariantArrayToString(VarPacket);
    //FreeDataPacket(DataPacket);
    end;

    function XMLToDelta(XMLData:String):OleVariant;
    var Delta:OleVariant;
        DataPacket: TDataPacket;
        FDSBase: IDSBase;
    begin
    Delta:=StringToVariantArray(XMLData);
    SafeArrayCheck(SafeArrayCopy(VarToDataPacket(Delta), DataPacket));
    CreateDbClientObject(CLSID_DSBase, IDSBase, FDSBase);
    Check(FDSBase,FDSBase.AppendData(DataPacket, True));
    FDSBase.SetProp(dspropXML_STREAMMODE, xmlOFF);
    Check(FDSBase,FDSBase.StreamDS(DataPacket));
    DataPacketToVariant(DataPacket, Result);
    //FreeDataPacket(DataPacket);
    end;

    end.

  • 相关阅读:
    ionic3 生命周期
    java基础中this,super
    LeetCode #26 Remove Duplicates from Sorted Array
    LeetCode #24 Swap Nodes in Pairs
    LeetCode #22 Generate Parentheses
    LeetCode #20 Valid Parentheses
    LeetCode #14 Longest Common Prefix
    iOS CAShapeLayer、CADisplayLink 实现波浪动画效果
    iOS CAEmitterLayer 实现粒子发射动画效果
    iOS CAReplicatorLayer 实现脉冲动画效果
  • 原文地址:https://www.cnblogs.com/chenmfly/p/4496377.html
Copyright © 2020-2023  润新知