[ASP.NET] RDLC 子報表運行模式

  • 8708
  • 0
  • 2012-03-27

[RDLC]子報表運行模式

記錄一下最近處理一個CASE的經驗

 

在開發RDLC子報表時,大致上的程式碼會是如下


	
//顯示Report的Method
private void Show_Report()
{
    ....
    ....
    
    //指定子報表事件處理常式
    this.ReportViewer1.LocalReport.SubreportProcessing += new Microsoft.Reporting.WebForms.SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

    //Run Query SQL 存入datatable,然後指定給RDLC報表檔 (程式碼略…..)
    ….
    ….
    this.ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("XXXXX", dt));
    this.ReportViewer1.LocalReport.Refresh();
}

void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
    // Exec 子報用的Query SQL 存入datatable,然後指定給RDLC報表檔 (程式碼略…..)
    ......
    ......
    
    e.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("XXXXX", dt);
}

 

然而值得注意的是,這樣的方式在LocalReport_SubreportProcessing裡是每一頁主報表的資料都會進去Query一次子報用的Query SQL

,所以當你把報表設計成不分頁的情況下,會變成像是在一個Loop裡不斷的Query SQL,所以報表資料愈多,Query愈多次,效能肯定不會太好

 

 

因此較好的寫法可以改成如下的方式,原理就是減少重覆執行Query的動作

 


	
private void Show_Report()
{

  //一次Query主報表及子報表要用的資料,放入所屬的DataTable
  //master_dt (程式碼略...)
  //sub_dt (程式碼略...)
  
  //指定子報表事件處理常式
   this.ReportViewer1.LocalReport.SubreportProcessing += new Microsoft.Reporting.WebForms.SubreportProcessingEventHandler(LocalReport_SubreportProcessing);

    this.ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("xxx", master_dt));
    this.ReportViewer1.LocalReport.Refresh();

}

void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
    //利用Sub DataTable的資料,直接指定給子報用就好,不再重覆Query
    e.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("xxxxx", sub_dt);
}

 

若本文對您有所幫助,歡迎轉貼,但請在加註【轉貼】及來源出處,並在附上本篇的超連結,感恩您的配合囉。

By No.18