建立專案並從 RDL 架構產生類別之後,您就可以從報表伺服器載入報表定義。
載入報表定義
在
ReportUpdater類別的頂端新增一個私用欄位(如果您使用的是 Visual Basic,則是模組)以用於Report類別。 此欄位將用來在應用程式的存留期間,維護從報表伺服器載入報表的參考。private Report _report;Private m_report As Report將 Program.cs 檔案中
LoadReportDefinition()方法的程式碼(Module1.vb for Visual Basic)替換為以下程式碼:private void LoadReportDefinition() { System.Console.WriteLine("Loading Report Definition"); string reportPath = "/AdventureWorks 2012 Sample Reports/Company Sales 2012"; // Retrieve the report definition // 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 2012 Sample Reports/Company Sales 2012" 'Retrieve the report definition '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 課:以程式設計方式更新報表定義。