6

标题基本概括了所有内容。我正在使用 TClientDataset 来存储对象数组,其中一个对象具有定义为一枚举类型的成员。据我了解,Delphi 集是位域,其大小可以从 1 到 32 个字节不等,具体取决于它们包含的数据量,而 Delphi 没有定义 TSetField。我应该使用什么样的字段来加载这个值?

4

3 回答 3

14

您可以使用 TBytesField 或 TBlobField

ClientDataSet1MySet:TBytesField,大小=32

var
  MySet: set of Byte;
  Bytes: array of Byte;
begin
  MySet := [1, 2, 4, 8, 16];

  // Write
  Assert(ClientDataSet1MySet.DataSize >= SizeOf(MySet), 'Data field is too small');

  SetLength(Bytes, ClientDataSet1MySet.DataSize);
  Move(MySet, Bytes[0], SizeOf(MySet));
  ClientDataSet1.Edit;
  ClientDataSet1MySet.SetData(@Bytes[0]);
  ClientDataSet1.Post;

  // Read
  SetLength(Bytes, ClientDataSet1MySet.DataSize);
  if ClientDataSet1MySet.GetData(@Bytes[0]) then
    Move(Bytes[0], MySet, SizeOf(MySet))
  else
    MySet := []; // NULL
end;
于 2008-12-07T14:02:08.930 回答
2

您可以将它们转换为字节,如下所示:

var
  States : TUpdateStatusSet; // Can be any set, I took this one from DB.pas unit
  SetAsAInteger: Integer;
  dbs: Pointer; // Here's the trick
begin
  States := [usModified, usInserted]; // Putting some content in that set
  dbs := @States;
  SetAsAInteger := PByte(dbs)^;
  //Once you got it, SetAsAInteger is just another ordinary integer variable.
  //Use it the way you like.
end;

从任何地方恢复:

var
  MSG: string;
  Inserted, Modified: string;
  States: TUpdateStatusSet;
  MySet: Byte;

begin
  while not ClientDataSet.Eof do
  begin
    //That's the part that interest us
    //Convert that integer you stored in the database or whatever 
    //place to a Byte and, in the sequence, to your set type.
    iSet := Byte(ClientDatasetMyIntegerField);// Sets are one byte, so they
                                              //  fit on a byte variable  
    States := TUpdateStatusSet(iSet);
    //Conversion finished, below is just interface stuff


    if usInserted in States then
      Inserted := 'Yes';
    if usModified in States then
      Modified := 'Yes';
    MSG := Format('Register Num: %d. Inserted: %s. Modified:%s',
                  [ClientDataSet.RecNo, Inserted, Alterted]);
    ShowMessage( MSG );
    ClientDataset.Next;
  end;

end;
于 2008-12-08T21:17:03.950 回答
0

基于安德烈亚斯的例子,但恕我直言,更加简单明了。

在 XE2 上测试

您可以使用 TBytesField 或 TBlobField

ClientDataSet1MySet:TBytesField,大小=32

1) 写作

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) 阅读

var
  MyResultSet: set of Byte;
begin
  Move(ClientDataSet1Test.AsBytes[0], MyResultSet, ClientDataSet1Test.DataSize);
end;
于 2013-12-05T08:39:32.730 回答