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

新建日期: 2006 年 7 月 17 日

创建项目并从 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 Sample Reports/Company Sales";
    
        // Retrieve the report defintion 
        // from the report server
        byte[] bytes = 
            _reportService.GetReportDefinition(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 Sample Reports/Company Sales"
    
        'Retrieve the report defintion 
        'from the report server
        Dim bytes As Byte() = _
            m_reportService.GetReportDefinition(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 架构生成的类更新报表

其他资源

Report Definition Language

帮助和信息

获取 SQL Server 2005 帮助