第 3 课:从报表服务器加载报表定义

创建项目并从 RDL 架构生成类后,即可从报表服务器加载报表定义。

加载报表定义

  1. ReportUpdater 类(如果是使用 Visual Basic,则为模块)的顶部添加一个私有字段,作为 Report 类的一部分。 此字段将用于维持从报表服务器加载的报表在应用程序生命周期内的引用。

    private Report _report;  
    
    Private m_report As Report  
    
  2. 将 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 课:以编程方式更新报表定义

另请参阅

使用从 RDL 架构生成的类更新报表 (SSRS 教程)
报表定义语言 (SSRS)