绑定不带登录代码的报表
在“项目设置”中,已经将 CrystalReportViewer 控件放置到了 Web 或 Windows 窗体上。在上一步中又给项目添加了 NorthwindCustomers 报表。
在本节中,将把 NorthwindCustomers 报表的文件目录路径绑定到 CrystalReportViewer 控件。然后,测试在没有设置数据库登录代码的情况下能否正确显示该报表。
将 NorthwindCustomers 报表的文件目录路径绑定到 CrystalReportViewer 控件
打开 Web 或 Windows 窗体。
从“视图”菜单中,单击“代码”。
找到在“项目设置”中创建的 ConfigureCrystalReports() 方法。
声明一个字符串变量,将其命名为“reportPath”,然后将一个本地报表的运行时路径赋值给它。对于网站项目和 Windows 项目,确定此路径时会有所不同:
对于网站,要将本地报表文件的名称作为字符串参数传递到 Server.MapPath() 方法中。这样,在运行时本地报表就会映射到硬盘文件目录路径。
Dim reportPath As String = Server.MapPath("NorthwindCustomers.rpt")
string reportPath = Server.MapPath("NorthwindCustomers.rpt");
对于 Windows 项目,要将 Application.StartupPath 属性与一个反斜杠和本地报表文件名称连接起来。这样,报表将映射到与 Windows 可执行文件相同的目录。
注意
编译时,需要将报表复制到可执行文件所在的目录。
Dim reportPath As String = Application.StartupPath & "\" & "NorthwindCustomers.rpt"
string reportPath = Application.StartupPath + "\\" + "NorthwindCustomers.rpt";
将 NorthwindCustomers 报表的文件目录路径赋给 CrystalReportViewer 控件的 ReportSource 属性。
myCrystalReportViewer.ReportSource = reportPath
crystalReportViewer.ReportSource = reportPath;
测试 NorthwindCustomers 报表的加载过程
现在即可生成并运行项目。预计报表加载将失败,因为此时尚未编写数据库登录代码。
从“生成”菜单中选择“生成解决方案”。
如果生成过程中出错,请立即纠正。
如果在 Windows 项目中使用非嵌入式报表,请在 \bin\debug\ 子目录中找到编译后的 Windows 可执行文件,然后将报表复制到该子目录中。
注意
要让 Windows 可执行文件在运行时加载非嵌入式报表,该报表必须与 Windows 可执行文件存储在同一个目录中。
从“调试”菜单中,单击“开始”。
NorthwindCustomers 报表并不显示。它会在添加数据库登录代码后显示。
<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 版本不同而不同。例如,如果安装了 Crystal Reports 10 和更高版本,则会提示您输入该报表的数据库登录信息。如果运行的是较早版本的 Crystal Reports,则会引发异常。不论是哪种情况,都需要依照以下过程来创建功能完整的应用程序。</p></td>
</tr>
</tbody>
</table>
- 返回到 Visual Studio,然后单击“停止”从调试模式中退出。