创建项目并从 RDL 架构生成类后,即可从报表服务器加载报表定义。
加载报表定义
在
ReportUpdater类(如果是使用 Visual Basic,则为模块)的顶部添加一个私有字段,作为Report类的一部分。 此字段将用于维持从报表服务器加载的报表在应用程序生命周期内的引用。private Report _report;Private m_report As Report将 Program.cs 文件中
LoadReportDefinition()方法的代码(对于 Visual Basic,使用 Module1.vb)替换为以下代码:private void LoadReportDefinition() { System.Console.WriteLine("Loading Report Definition"); string reportPath = "/AdventureWorks 2012 Sample Reports/Company Sales 2012"; // Retrieve the report definition // from the report server byte[] bytes = _reportService.GetItemDefinition(reportPath); if (bytes != null) { XmlSerializer serializer = new XmlSerializer(typeof(Report)); // Load the report bytes into a memory stream using (MemoryStream stream = new MemoryStream(bytes)) { // Deserialize the report stream to an // instance of the Report class _report = (Report)serializer.Deserialize(stream); } } }Private Sub LoadReportDefinition() System.Console.WriteLine("Loading Report Definition") Dim reportPath As String = _ "/AdventureWorks 2012 Sample Reports/Company Sales 2012" 'Retrieve the report definition 'from the report server Dim bytes As Byte() = _ m_reportService.GetItemDefinition(reportPath) If Not (bytes Is Nothing) Then Dim serializer As XmlSerializer = _ New XmlSerializer(GetType(Report)) 'Load the report bytes into a memory stream Using stream As MemoryStream = New MemoryStream(bytes) 'Deserialize the report stream to an 'instance of the Report class m_report = CType(serializer.Deserialize(stream), _ Report) End Using End If End Sub
下一课
在下一课中,你将编写代码来更新从报表服务器加载的报表定义。 请参阅 第 4 课:以编程方式更新报表定义。