2010-01-21 66 views
0

我在遗留代码中使用带有单向属性= False的TIBQuery(Interbase)有很多方法。问题在于用户有时会出现内存异常。 我怀疑可以通过将此属性设置为True来解决此问题,因为不需要缓存记录。TIBQuery.Unidirectional = 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; 

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

回答

1

如果同一个实例使用了许多SQL查询,您主要关注的是不破坏应用程序,在需要时,可以节省单向属性的值,将它设置为True,使用对象,并使其恢复到最后的原始价值。

喜欢的东西:

var 
    OldUnnidirectionalValue: Boolean; 
begin 
    OldUnnidirectionalValue := qryReport.Unnidirectional; 
    qryReport.Unnidirectional := True; 
    try 
    qryReport.SQL.Text := 'select what you want'; 
    qryReport.Open; 
    try 
     while not qryReport.eof do 
     begin 
     UseTheRecord; 
     qryReport.Next; 
     end; 
    finally 
     qryReport.Close; 
    end; 
    finally 
    qryReport.Unnidirectional := OldUnnidirectionalValue; 
    end; 
end; 

避免关闭打开序列,必须使用try/finally块使用后一直关闭查询。

此致敬礼;)

+0

看起来很合理,谢谢你的例子。 – 2010-01-22 07:48:27

7

一旦您设置单向为True,则只能拨打第一个和下。该代码一直不好。您绝不应该使用RecordCount来遍历记录。使用下一个和EOF。通过这种方式,您无需调用Last来强制数据库加载整个结果集以便获取有多少条记录到

+0

感谢您的答案!我会尝试。 – 2010-01-21 12:58:48

+0

+1我同意Luigi – 2010-01-22 16:37:36