Condividi tramite


Lezione 3: Caricamento della definizione di un report dal Server report

Dopo aver creato il progetto e generato le classi dallo schema RDL, è possibile caricare la definizione di un report dal server di report.

Per caricare la definizione di un report

  1. Aggiungere un campo privato all'inizio della classe ReportUpdater (modulo se si utilizza Visual Basic) per la classe Report. Questo campo verrà utilizzato per mantenere un riferimento al report caricato dal server di report per tutta la durata dell'applicazione.

    private Report _report;
    
    Private m_report As Report
    
  2. Sostituire il codice per il metodo LoadReportDefinition() nel file Program.cs (Module1.vb per Visual Basic) con il codice seguente:

    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
    

Lezione successiva

Nella lezione successiva si scriverà il codice per l'aggiornamento della definizione del report caricata dal server di report. Vedere Lezione 4: Aggiornamento della definizione del report a livello di programmazione.