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

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

加载报表定义

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

    private Report _report;
    
    Private m_report As Report
    
  2. 用以下代码替换 Program.cs 文件(对于 Visual Basic,为 Module1.vb)中 LoadReportDefinition() 方法的代码:

    private void LoadReportDefinition()
    {
        System.Console.WriteLine("Loading Report Definition");
    
        string reportPath = 
            "/AdventureWorks 2008 Sample Reports/Company Sales 2008";
    
        // Retrieve the report defintion 
        // 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 2008 Sample Reports/Company Sales 2008"
    
        'Retrieve the report defintion 
        '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 课:以编程方式更新报表定义