2010-01-21 64 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