第 4 课:以编程方式更新报表定义

从报表服务器加载报表定义并使用报表字段引用报表定义后,需要更新报表定义。 对于此示例,你将更新 Description 报表的属性。

更新报表定义

  1. 将 Program.cs 文件中 UpdateReportDefinition()方法的代码(Module1.vb for Visual Basic)替换为以下代码:

    private void UpdateReportDefinition()  
    {  
        System.Console.WriteLine("Updating Report Definition");  
    
        // Create a list of the Items Choices for the Report. The   
        // ItemsChoiceType118 enum represents all the properties  
        // available in the report and the ItemsElementName   
        // represents the properties that exist in the current   
        // instance of the report.  
        List<ItemsChoiceType118> _reportItems =   
            new List<ItemsChoiceType118>(_report.ItemsElementName);  
    
        // Locate the index for the Description property  
        int index = _reportItems.IndexOf(  
            ItemsChoiceType118.Description);  
    
        // The Description item is of type StringLocIDType, so   
        // cast the item type first and then assign new value.  
        System.Console.WriteLine("- Old Description: " +   
            ((StringLocIDType)_report.Items[index]).Value );  
    
        // Update the Description for the Report  
        ((StringLocIDType)_report.Items[index]).Value =   
            "New Report Description";  
    
        System.Console.WriteLine("- New Description: " +   
            ((StringLocIDType)_report.Items[index]).Value );  
    }  
    
    Private Sub UpdateReportDefinition()  
    
        System.Console.WriteLine("Updating Report Definition")  
    
        'Create a list of the Items Choices for the Report. The   
        'ItemsChoiceType118 enum represents all the properties  
        'available in the report and the ItemsElementName   
        'represents the properties that exist in the current   
        'instance of the report.  
        Dim reportItems As List(Of ItemsChoiceType118) = _  
            New List(Of ItemsChoiceType118)(m_report.ItemsElementName)  
    
        'Locate the index for the Description property  
        Dim index As Integer = _  
            reportItems.IndexOf(ItemsChoiceType118.Description)  
    
        'The Description item is of type StringLocIDType, so   
        'cast the item type first and then assign new value.  
        System.Console.WriteLine("- Old Description: " & _  
            DirectCast(m_report.Items(index), StringLocIDType).Value)  
    
        'Update the Description for the Report  
        DirectCast(m_report.Items(index), StringLocIDType).Value = _  
            "New Report Description"  
    
        System.Console.WriteLine("- New Description: " & _  
            DirectCast(m_report.Items(index), StringLocIDType).Value)  
    
    End Sub  
    

下一课

在下一课中,你将会把更新后的报告定义保存回报告服务器。 请参阅 第 5 课:将报表定义发布到报表服务器

另请参阅

使用从 RDL 架构生成的类更新报表 (SSRS 教程)