3

我需要浏览大量存储在备忘录字段中的悖论表中的数据。我需要逐行处理这个数据并处理每一行。

如何告诉 Delphi 逐一获取备忘录字段中的每一行?

我可以使用#13#10 作为分隔符吗?

4

2 回答 2

4

假设备忘录字段中的内容使用 #13#10 作为行分隔符,那么我将使用 aTStringList和非常有用的Text属性将备忘录字段文本拆分为单独的行:

var
  StringList: TStringList;
  Line: string;
.....
StringList.Text := MemoFieldText;
for Line in StringList do
  Process(Line);

即使您的备注字段使用 Unix 换行符,此代码也会正确解释备注字段。

于 2012-04-06T20:33:28.883 回答
1

这取决于在 Paradox 中实际声明该字段的方式。如果它是一个 TMemoField,这很容易:

var
  SL: TStringList;
  Line: string;
begin
  SL := TStringList.Create;
  try
    SL.Text := YourMemoField.GetAsString;
    for Line in SL do
     // Process each line of text using `Line`
  finally
    SL.Free;
  end;
end;

如果是 TBlobField,就复杂一点。您需要使用 a 读取备忘录字段TBlobStream,并将该流的内容加载到 a 中TStringList

// For Delphi versions that support it:
procedure LoadBlobToStringList(const DS: TDataSet; const FieldName: string;
  const SL: TStringList);
var
  Stream: TStream;
begin
  Assert(Assigned(SL), 'Create the stringlist for LoadBlobToStringList!');
  SL.Clear;
  Stream := DS.CreateBlobStream(DS.FieldByName(FieldName), bmRead);
  try
    SL.LoadFromStream(Stream);
  finally
    Stream.Free;
  end;
end;

// For older Delphi versions that do not have TDataSet.CreateBlobStream
procedure LoadBlobToStringList(const DS: TDataSet; const TheField: TField; 
  const SL: TStringList);
var
  BlobStr: TBlobStream;
begin
  Assert(Assigned(SL), 'Create the stringlist for LoadBlobToStringList!');
  SL.Clear;
  BlobStr := TBlobStream.Create(DS.FieldByName(TheField), bmRead);
  try
    SL.LoadFromStream(BlobStr);
  finally
    BlobStr.Free;
  end;
end;

// Use it
var
  SL: TStringList;
  Line: string;
begin
  SL := TStringList.Create;
  LoadBlobToStringList(YourTable, YourMemoFieldName, SL);
  for Line in SL do
    // Process each Line, which will be the individual line in the blob field

  // Alternatively, for earlier Delphi versions that don't support for..in
  // declare an integer variable `i`
  for i := 0 to SL.Count - 1 do
  begin
    Line := SL[i];
    // process line of text using Line
  end;
end;
于 2012-04-06T20:52:15.420 回答