共用方式為


第 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 課:以程式設計方式更新報表定義>。