TIBQuery.Unidirectional = True.我该如何重写代码?

Rol*_*son 0 delphi activerecord

我在遗留代码中有许多使用带有单向属性= False的TIBQuery(Interbase)的方法.问题是用户有时会出现内存异常.我怀疑它可以通过将此属性设置为True来修复,因为不需要缓存记录.

当然我不想打破旧代码,但我也想解决这个问题.

这是一个代码示例(由于大小不完整):

procedure TAnalyzeForm.CostByInvoice;
begin
  try
    qryReport.Close;
    qryReport.Open;
    qryReport.Last;
    qryReport.First;
    if qryReport.RecordCount > 0 then
    begin
      for i := 0 to qryReport.RecordCount - 1 do
      begin
        vInvoiceNo := Format('%-8s', [qryReport.FieldValues['InvoiceNo']]);
        vDeptId := Format('%8s', [qryReport.FieldValues['DepartmentId']]);
        vOrgName := Format('%-22s', [qryReport.FieldValues['OrgName']]);
        vInvDate := qryReport.FieldValues['InvoiceDate'];
        vInvNetCur := qryReport.FieldValues['InvNetCur'];
        vInvVatCur := qryReport.FieldValues['InvVatCur'];
        vInvTotCur := qryReport.FieldValues['InvTotCur'];
        vInvCur := qryReport.FieldValues['UnitId'];
        vTotNet := vTotNet + qryReport.FieldValues['InvNetValue'];
        vTotVat := vTotVat + qryReport.FieldValues['InvVatValue'];
        vTotTot := vTotTot + (qryReport.FieldValues['InvNetValue'] + qryReport.FieldValues['InvVatValue']);
        grdCost.Cells[1, i+1] := vInvoiceNo;
        grdCost.Cells[2, i+1] := vDeptId + ' ' + vOrgName;
        grdCost.Cells[3, i+1] := FormatDateTime('dd.mm.yyyy', vInvDate);
        grdCost.Cells[4, i+1] := Format('%12.2f', [vInvNetCur]);
        grdCost.Cells[5, i+1] := Format('%12.2f', [vInvVatCur]);
        grdCost.Cells[6, i+1] := Format('%12.2f', [vInvTotCur]);
        grdCost.Cells[7, i+1] := 'EUR';
        grdCost.RowCount := i+1 + 1;
        qryReport.next;
      end;
      txtNetCost.Caption := Format('%12.2f', [vTotNet]);
      txtVatCost.Caption := Format('%12.2f', [vTotVat]);
      txtTotCost.Caption := Format('%12.2f', [vTotTot]);
      SummaryInfo(stSummaryInfoCost, 'Number of costinvoices: ' + IntToStr(qryReport.RecordCount), time, true);
    end
    else
      MessageDlg('nothing found!', mtInformation, [mbOk], 0);
  finally
    qryReport.Close;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

重要的变量是qryReport,它是一个TIBQuery.我想重写它,所以我可以设置TIBQuery.Unidirectional = True.qryReport在许多具有不同SQL的地方重用,所以我认为这就是开始时关闭,打开序列的原因.

小智 7

将Unidirectional设置为True后,您只能调用First和Next.那段代码一直很糟糕.您永远不应该使用RecordCount迭代记录.使用Next和EOF.这样您就不需要调用Last来强制数据库加载整个结果集只是为了获取有多少记录.