在本节中,将创建一个帮助器方法,它通过Crystal Reports Web 服务访问服务器并生成报表列表。
**注意 **此过程要求您具有已配置为使用 Crystal Reports Web 服务的计算机的访问权限。在继续操作之前,请确保已正确配置您的计算机。有关更多信息,请参见在 Crystal 服务中配置服务器文件。
创建帮助器方法以访问 Crystal Reports Web 服务
打开 Web 或 Windows 窗体。
从“视图”菜单中,单击“代码”。
添加三个专用类级变量。
[Visual Basic]
Private myServerFileReport As ServerFileReport Private myReportManagerRequest As ReportManagerRequest Private myServerFileReportManagerProxy As ServerFileReportManagerProxy
[C#]
private ServerFileReport serverFileReport; private ReportManagerRequest reportManagerRequest; private ServerFileReportManagerProxy serverFileReportManagerProxy;
创建一个返回
ArrayList的新专用帮助器方法。[Visual Basic]
Protected Function getReports() As ArrayList End Function
[C#]
protected ArrayList getReports() { }在帮助器方法内部,实例化一个新的
ServerFileReport实例。[Visual Basic]
myServerFileReport = New ServerFileReport()
[C#]
serverFileReport = new ServerFileReport();
将
ServerFileReport实例的ReportPath属性设置为空字符串。[Visual Basic]
myServerFileReport.ReportPath = ""
[C#]
serverFileReport.ReportPath = "";
将
ServerFileReport的WebServiceURL属性设置为所安装的 Crystal Reports 版本的查看器虚拟目录,请参见查看器虚拟目录。在此示例代码中,为 Crystal Reports for Visual Studio 2005 配置了查看器的虚拟目录。
[Visual Basic]
myServerFileReport.WebServiceUrl = "https://localhost:80/CrystalReportsWebServices2005/serverfilereportservice.asmx"
[C#]
serverFileReport.WebServiceUrl = "https://localhost:80/CrystalReportsWebServices2005/serverfilereportservice.asmx";
**注意 **本教程假定报表服务器是本地计算机,因此,您使用
https://localhost:80/作为 Web 服务 URL。要使用不同计算机作为报表服务器,请用报表服务器的地址和端口号替换https://localhost:80/。实例化一个新的
ReportManagerRequest实例。[Visual Basic]
myReportManagerRequest = New ReportManagerRequest()
[C#]
reportManagerRequest = new ReportManagerRequest();
将
serverFileReport实例的GetExtraData方法的结果赋值给ReportManagerRequest实例的ExtraData属性。这会将ExtraData属性设置为 Web 服务 URL。[Visual Basic]
myReportManagerRequest.ExtraData = myServerFileReport.GetExtraData()
[C#]
reportManagerRequest.ExtraData = serverFileReport.GetExtraData();
将
serverFileReport实例的ToUri方法的结果赋值给ReportManagerRequest实例的ParentUri属性。这将指定服务器计算机上包含要列出的报表的目录。[Visual Basic]
myReportManagerRequest.ParentUri = myServerFileReport.ToUri()
[C#]
reportManagerRequest.ParentUri = serverFileReport.ToUri();
实例化一个新的
ServerFileReportManagerProxy实例。[Visual Basic]
myServerFileReportManagerProxy = New ServerFileReportManagerProxy()
[C#]
serverFileReportManagerProxy = new ServerFileReportManagerProxy();
将
ServerFileReportManagerProxy的Url属性设置为所安装的 Crystal Reports 版本的服务器文件报表管理器,请参见查看器虚拟目录。在此示例代码中,为 Crystal Reports for Visual Studio 2005 配置了查看器的虚拟目录。
[Visual Basic]
myServerFileReportManagerProxy.Url = "https://localhost:80/CrystalReportsWebServices2005/serverfilereportmanager.asmx"
[C#]
serverFileReportManagerProxy.Url = "https://localhost:80/CrystalReportsWebServices2005/serverfilereportmanager.asmx";
创建并实例化一个新的
ReportManagerResponse对象。[Visual Basic]
Dim myReportManagerResponse As ReportManagerResponse = new ReportManagerResponse()
[C#]
ReportManagerResponse reportManagerResponse = new ReportManagerResponse();
调用
serverFileReportManagerProxy实例的ListChildObjects()方法,并将结果分配给ReportManagerResponse实例。ListChildObjects方法将返回一个列表,其中包含位于报表服务器上的文件。[Visual Basic]
myReportManagerResponse = myServerFileReportManagerProxy.ListChildObjects(myReportManagerRequest)
[C#]
reportManagerResponse = serverFileReportManagerProxy.ListChildObjects(reportManagerRequest);
创建并实例化一个新的
ArrayList。[Visual Basic]
Dim myRemoteReports As ArrayList = New ArrayList()
[C#]
ArrayList remoteReports = new ArrayList();
下一步,创建
For Each循环,此循环在reportManagerResponse的每个元素间循环。[Visual Basic]
For Each myReportUriString As String In myReportManagerResponse.ReportUris
Next
\[C\#\]<pre class="code" IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">foreach (string reportUriString in reportManagerResponse.ReportUris)
{ }
在
For Each循环内部,创建一个新的serverFileReport实例。[Visual Basic]
myServerFileReport = New ServerFileReport()
[C#]
serverFileReport = new ServerFileReport();
将
serverFileReportUri 设置为reportUriString值。[Visual Basic]
myServerFileReport = ServerFileReport.FromUri(myReportUriString)
[C#]
serverFileReport = ServerFileReport.FromUri(reportUriString);
仍然在
For Each循环内部,创建一个If条件块,用于验证ServerFileReport的ObjectType属性是否为报表类型。**注意 **
ListChildObjects方法将返回目录和报表。在本教程中,您将筛选报表文件的对象列表。[Visual Basic]
If (myServerFileReport.ObjectType = EnumServerFileType.REPORT) Then End If
[C#]
if (serverFileReport.ObjectType == EnumServerFileType.REPORT) { }在
If条件块内部,将reportUriString实例添加到remoteReport ArrayList中。[Visual Basic]
myRemoteReports.Add(myReportUriString)
[C#]
remoteReports.Add(reportUriString);
最后,在
For Each循环外部,返回remoteReports ArrayList。[Visual Basic]
Return myRemoteReports
[C#]
return remoteReports;
在“文件”菜单上单击“全部保存”。
在下一步中,将编写代码,使用报表列表自动填充“DropDownList”或“ComboBox”控件。
对于网站,继续前进到填充在网站中的 DropDownList 控件。
对于 Windows 项目,继续前进到填充在 Windows 项目中的 ComboBox 控件。
请参见
教程:填充 Web 服务的报表的下拉列表 | 教程和示例代码
© 2005 Business Objects SA. All Rights Reserved
| Business Objects http://www.china.businessobjects.com/ や穿狝叭 http://www.china.businessobjects.com/BOindex/support/ |