Leçon 3 : chargement d'une définition de rapport à partir du serveur de rapports

Après avoir créé votre projet et généré les classes à partir du schéma RDL, vous êtes prêt à charger une définition du rapport depuis le serveur de rapports.

Pour charger une définition de rapport

  1. Ajoutez un champ privé au début de la classe (ou du module en Visual Basic) ReportUpdater pour la classe Report. Ce champ sera utilisé pour conserver une référence au rapport chargé depuis le serveur de rapports pendant la vie de l'application.

    private Report _report;
    
    Private m_report As Report
    
  2. Remplacez le code de la méthode LoadReportDefinition() du fichier Program.cs (Module1.vb en Visual Basic) par le code suivant :

    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
    

Leçon suivante

Dans la leçon suivante, vous allez écrire le code permettant de mettre à jour la définition du rapport chargée depuis le serveur de rapports. Voir Leçon 4 : mise à jour par programme de la définition du rapport.