绑定到已加载到缓存管理实用程序类的非嵌入式报表

对象模型

此报表绑定方案使用 ReportDocument(请参见“通过 ReportDocument 对象模型进行报表绑定”)。

报表的位置

报表位于文件目录中。

说明

此报表绑定方案类似于以下报表绑定方案:“ 绑定到缓存的嵌入式报表类 ”。此方案的不同之处在于报表未嵌入。有关非嵌入式报表的更多信息,请参见“应该使用嵌入式报表还是非嵌入式报表?”

Crystal Reports for Visual Studio 提供下列两种有助于用 ASP.NET Cache 对象缓存报表的功能:

  • 一个内置报表缓存管理框架。当相同报表具有唯一参数或者具有要求每个被缓存的实例都要有唯一密钥的登录凭据时,该框架能对此进行识别。
  • ICachedReport 接口,该接口向报表缓存管理框架标识报表缓存管理实用程序类。

“ 绑定到缓存的嵌入式报表类 ”中,您了解了在项目中嵌入报表时自动创建的 Cached[report name] 类。不过,您也可以手动创建管理非嵌入式报表的缓存管理实用程序类。有关代码示例,请参见下面的“实现”一节。

有关如何缓存报表和使用缓存管理实用工具类的详细信息,请参见“应该使用常规报表还是缓存报表?”

缓存的使用

缓存具有有限、特定的用法,如果不小心管理,可能会过度使用系统资源。若要了解应该在何时使用缓存,请参见“缓存具有“高可共享性”的报表”

优点

  • 为可共享性设计:非常适合存储具有高可共享性且参数或登录信息中很少发生改变的报表。
  • 优化数据访问:如果具有高可共享性的报表非常大,或具有非常复杂以致于需要几分钟来进行检索的查询,则使用缓存管理实用程序类可以更快地访问数据。

缺点

  • 服务器负担加重:保留在 ASP.NET Cache 对象中的报表将加重服务器上的内存资源负担。
  • 持久性问题:缓存具有一些依赖项,这些依赖项允许它检查报表实例中的更改并重新缓存报表实例。但是,如果数据库发生更改,则 Cache 中的报表实例将不能刷新以显示该更改。
  • 消耗资源:如果报表带有用不同的参数字符串频繁调用的参数(特别是如果这些参数中有一个是用户 ID),则每次都会产生一个新的缓存报表。这将消耗大量系统资源。如果报表的共享程度不是很高,则应该将该报表实例赋给 Session 对象。请参见 “Session 和 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/8yfdxzdx.alert_note(zh-cn,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 的字符串实例和一个名为 nonEmbeddedReportDocument 的 ReportDocument 实例。
``` 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() 方法并返回空值。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>从此方法返回空值将提示 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. 若要查看该报表,请生成并运行您的项目。

请参见