繫結至已載入快取管理公用程式類別中的非內嵌報表

物件模型

這個報表繫結案例使用 ReportDocument (請參閱「與 ReportDocument 物件模型繫結的報表」)。

報表位置

報表位於檔案目錄。

說明

這個報表繫結案例類似於報表繫結案例「 繫結至快取的內嵌報表類別 」。與這個案例的差異是其報表並非內嵌的。如需非內嵌報表的詳細資訊,請參閱「我應該使用內嵌或非內嵌的報表?」

Crystal Reports for Visual Studio 提供兩種功能,以輔助您使用 ASP.NET Cache 物件來快取報表:

  • 內建的報表快取管理架構,可以辨識相同的報表何時具有唯一參數或登入憑證,以對快取中每一執行個體要求其唯一的識別碼。
  • ICachedReport 介面,讓報表快取管理架構得以識別報表快取管理公用程式類別。

「 繫結至快取的內嵌報表類別 」中,您已學到會在報表嵌入專案時自動建立的 Cached[報表名稱] 類別。但您可以手動建立用來管理非內嵌報表的快取管理公用程式類別。如需程式碼範例,請參閱下面<實作>一節。

如需如何快取報表與使用快取管理公用程式類別的詳細資訊,請參閱「我應該使用一般或快取報表?」

快取的使用

快取的使用要加以限制與規範,如果沒有謹慎管理,可能會造成過度使用系統資源。若要瞭解使用快取的時機,請參閱「以「高度共用性」快取報表」

優點

  • 專為共用的目的而設計:非常適合用來儲存具有高度共用性且其參數或登入資訊少有變化的報表。
  • 最佳化資料存取:如果具高度共用性的報表非常龐大,或是它有複雜到需要數分鐘來擷取資料的查詢,使用快取管理公用程式類別存取資料會比較快速。

缺點

  • 伺服器負載加重:報表留在 ASP.NET 快取物件中會使伺服器的記憶體資源相當吃重。
  • 保存性問題:快取具有某種相依性,允許其檢查報表執行個體內的變更,然後重新快取報表執行個體。但如果資料庫有所變更,快取中的報表執行個體並不會重新整理顯示該變更。
  • 消耗資源:如果報表含有經常要用不同參數字串進行呼叫的參數 (特別是其中一個參數為使用者 ID 時),就表示每次都得產生新的快取報表。這樣相當消耗系統資源,如果無法充分共用報表,報表執行個體即應轉而指派給工作階段物件。請參閱「ReportDocument 物件模型的工作階段和保存性」

快取非內嵌報表後將其繫結至 CrystalReportViewer 控制項

Note附註

這個程序只能與「專案設定」一節中建立的專案搭配使用。「專案設定」包含特定命名空間參考以及本程序所需的程式碼組態;您必須具備這個組態,否則將無法完成此程序。因此,在您開始本程序前,必須先遵循「專案設定」中的步驟進行。

  1. 在 General Business 子目錄中尋找 World Sales Report.rpt 檔案。如需範例報表的詳細資訊,請參閱「範例報表的目錄」

  2. 將包含 World Sales Report.rpt 的完整檔案目錄路徑複製到剪貼簿。

  3. 在您已於「專案設定」時建立的 ConfigureCrystalReports() 方法中,宣告 reportPath 字串變數,並設定為您在上一個步驟複製的 World Sales Report 檔案目錄路徑字串。

``` vb
Dim reportPath As String = _
"C:\Program Files\Microsoft Visual Studio 9.0\" _
& "Crystal Reports\Samples\En\Reports\General Business\" _
& "World Sales Report.rpt"
```

``` csharp
string reportPath =
"C:\\Program Files\\Microsoft Visual Studio 9.0\\"
+ "Crystal Reports\\Samples\\En\\Reports\\General Business\\"
+ "World Sales Report.rpt";
```
  1. 宣告 NonEmbeddedReportCacher 類別,同時傳入 reportFile 字串變數以產生其執行個體。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images\36bhtx7w.alert_note(zh-tw,VS.90).gif" alt="Note" class="note" />附註</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>若要瞭解如何建立 NonEmbeddedReportCacher 類別,請參閱本逐步解說程序的下一個程序。</p></td>
</tr>
</tbody>
</table>

``` vb
Dim myNonEmbeddedReportCacher As NonEmbeddedReportCacher = _New NonEmbeddedReportCacher(reportFile)
```

``` csharp
NonEmbeddedReportCacher nonEmbeddedReportCacher = new NonEmbeddedReportCacher(reportFile);
```
  1. 將報表快取管理公用程式類別執行個體指派給 CrystalReportViewer 控制項的 ReportSource 屬性。
``` vb
myCrystalReportViewer.ReportSource = myNonEmbeddedReportCacher
```

``` csharp
crystalReportViewer.ReportSource = nonEmbeddedReportCacher;
```

建立 NonEmbeddedReportCacher 快取管理公用程式類別

  1. 在專案中建立新類別,命名為 NonEmbeddedReportCacher。
``` vb
Public Class NonEmbeddedReportCacher

End Class
```

``` csharp
using System;

namespace MyWebApplication
{
public class NonEmbeddedReportCacher
{
public NonEmbeddedReportCacher()
{
}
}
}
```
  1. 將 ICachedReport 介面附加至類別簽名碼。
``` vb
Public Class NonEmbeddedReportCacher
Implements ICachedReport
```

``` csharp
public class NonEmbeddedReportCacher : ICachedReport
```
  1. 在類別頂端加入三行 "Imports" [Visual Basic] 或 "using" [C#] 陳述式。
``` vb
Imports CrystalDecisions.Shared
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.CrystalReports.Engine
```

``` csharp
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;
```
  1. 在類別內宣告兩個類別層級的執行個體:字串執行個體 (名為 reportFileName) 和 ReportDocument 執行個體 (名為 nonEmbeddedReportDocument)。
``` vb
Private reportFileName As String
Private nonEmbeddedReportDocument As ReportDocument
```

``` csharp
private string reportFileName;
private ReportDocument nonEmbeddedReportDocument;
```
  1. 設定建構函式接受 reportFileName 字串,並在建構函式內將該字串指派給 reportFileName 類別變數。

    Public Sub New(ByVal reportFileName As String)
    Me.reportFileName = reportFileName
    End Sub
    
    public NonEmbeddedReportCacher(string reportFileName)
    {
    this.reportFileName = reportFileName;
    }
    

    接下來的步驟都是在實作介面所需要的屬性或方法:

    • IsCacheable
    • ShareDBLogonInfo
    • CacheTimeOut
    • CreateReport()
    • GetCustomizedCacheKey(RequestContext request)
  2. 建立 IsCacheable 屬性,傳回值為 True。

``` vb
Public Overridable Property IsCacheable() As Boolean Implements ICachedReport.IsCacheable
Get
Return True
End Get
Set(ByVal Value As Boolean)
End Set
End Property
```

``` csharp
public virtual Boolean IsCacheable
{
get
{
return true;
}
set
{
}
}
```
  1. 建立 ShareDBLogonInfo 屬性,傳回值為 False。
``` vb
Public Overridable Property ShareDBLogonInfo() As Boolean Implements ICachedReport.ShareDBLogonInfo
Get
Return False
End Get
Set(ByVal Value As Boolean)
End Set
End Property
```

``` csharp
public virtual Boolean ShareDBLogonInfo
{
get
{
return false;
}
set
{
}
}
```
  1. 建立 CacheTimeOut 屬性,傳回 CachedReportConstants 類別所提供的常數。
``` vb
Public Overridable Property CacheTimeOut() As TimeSpan Implements ICachedReport.CacheTimeOut
Get
Return CachedReportConstants.DEFAULT_TIMEOUT
End Get
Set(ByVal Value As TimeSpan)
End Set
End Property
```

``` csharp
public virtual TimeSpan CacheTimeOut
{
get
{
return CachedReportConstants.DEFAULT_TIMEOUT;
}
set
{
}
}
```
  1. 建立 CreateReport() 方法,傳回已載入至類別層級 ReportDocument 執行個體的非內嵌報表。
``` vb
Public Overridable Function CreateReport() As ReportDocument Implements ICachedReport.CreateReport
If nonEmbeddedReportDocument Is Nothing Then
nonEmbeddedReportDocument = New ReportDocument()
nonEmbeddedReportDocument.Load(reportFileName)
End If
Return nonEmbeddedReportDocument
End Function
```

``` csharp
public virtual ReportDocument CreateReport()
{
if (nonEmbeddedReportDocument == null)
{
nonEmbeddedReportDocument = new ReportDocument();
nonEmbeddedReportDocument.Load(reportFileName);
}
return nonEmbeddedReportDocument;
}
```
  1. 建立 GetCustomizedCacheKey() 方法,傳回值為 Null。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images\36bhtx7w.alert_note(zh-tw,VS.90).gif" alt="Note" class="note" />附註</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>這個方法傳回 Null 即提示 Crystal Reports .NET SDK 要對快取查閱索引鍵本身加以管理。另一種方式則是在此處自行建立與加入自訂快取索引鍵方法。</p></td>
</tr>
</tbody>
</table>

``` vb
Public Overridable Function GetCustomizedCacheKey(ByVal request As RequestContext) As String Implements ICachedReport.GetCustomizedCacheKey
Return Nothing
End Function
```

``` csharp
public virtual String GetCustomizedCacheKey(RequestContext request)
{
return null;
}
```
  1. 若要檢視報表,請建置並執行專案。

請參閱