What's the best way to store a Delphi set in a dataset?

前端 未结 3 443
醉话见心
醉话见心 2021-02-06 12:29

The title pretty much says it all. I\'m using a TClientDataset to store an array of objects, and one of the objects has a member defined as a set of an enumerat

3条回答
  •  猫巷女王i
    2021-02-06 13:01

    Based on the example of Andreas, but made somewhat simpler and clearer IMHO.

    Tested on XE2

    You could use a TBytesField or a TBlobField

    ClientDataSet1MySet: TBytesField, Size=32

    1) Writing

    var
      MySet: set of Byte;
      Bytes: TBytes;
    begin
      MySet := [0];
    
      // Write
      Assert(ClientDataSet1Test.DataSize >= SizeOf(MySet), 'Data field is too small');
    
      SetLength(Bytes, ClientDataSet1Test.DataSize);
      Move(MySet, Bytes[0], SizeOf(MySet));
      ClientDataSet1.Edit;
      ClientDataSet1Test.AsBytes := Bytes;
      ClientDataSet1.Post;
    end;
    

    2) Reading

    var
      MyResultSet: set of Byte;
    begin
      Move(ClientDataSet1Test.AsBytes[0], MyResultSet, ClientDataSet1Test.DataSize);
    end;
    

提交回复
热议问题