65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 RDLC 和数据集开发 ASP.NET Reporting Services 报表

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (10投票s)

2009年7月5日

CPOL

1分钟阅读

viewsIcon

128297

开发和调用 RDLC 报表,无需 Reporting Services 服务器。

引言

在 ASP.NET 开发过程中,您可能会在使用 Reporting Services 时遇到一些困难或做出艰难的决定。您的 ASP.NET 应用程序访问报表是否真的需要安装并运行 Reporting Services 服务器?

答案是 RDLC,即 ASP.NET 应用程序中的 Reporting Services 报表文件。这些文件易于创建,但在创建布局后,当您尝试从 SQL Server 2005 数据库加载数据时,您会发现一个问题 - 如何将 DataSet 映射到我的报表?

在 RDL(通用服务器 Reporting Services)中,您可以直接执行此操作,但在 RDLC 中,您必须在 Web 应用程序/项目中创建 DataSet,并将正确的参数发送到报表,以便它可以从数据库获取相应的数据并在报表上显示。

听起来很简单,对吧?问题在于尝试找到这段代码……

背景

本文涵盖了如何将 DataSet 发送到 RDLC 文件以显示带有数据的报表的现有差距。

使用代码

即使介绍或概念听起来很简单,您也必须仔细应用代码。

首先,将 ReportViewer 拖放到您的页面中(在代码中,我将其命名为 ReportViewer1)。

using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using Microsoft.Reporting.WebForms;

/// <summary>
/// Loads DataSources to the RDLC Report
/// </summary>
/// <param name="lineNr">Line Number
///  as parameter to get DataSources results from SQL DB</param>
protected void LoadReport(int lineNr)
{
    //  1. Clear Report Data
    ReportViewer1.LocalReport.DataSources.Clear();  

    //  2. Get Connection String from Web.Config
    SqlConnection sqlCon = new SqlConnection();
    sqlCon.ConnectionString = 
      ConfigurationManager.ConnectionStrings
      ["myConnection"].ConnectionString;

    //  3. Create DataSets (In my case I use 2 DataSets,
    //     you can use one or more)
    DataSet dsHeader = new DataSet();
    DataSet dsDetail = new DataSet();


    //  4. Select the DataSource to both DataSets
    // (In my case both SQL Stored Procedures
    //   use the same Sql parameter)

    SqlParameter sqlParm = new SqlParameter("@intNr", 
                                            lineNr.ToString());
    dsHeader = SqlHelper.ExecuteDataset(sqlCon, 
               CommandType.StoredProcedure, 
               "stp_S_LoadHeaderData",sqlParm);
    dsDetail = SqlHelper.ExecuteDataset(sqlCon, 
               CommandType.StoredProcedure, 
               "stp_S_LoadDetailData",sqlParm);

    //  5. The next lines mean that you will fill the datasets
    //     with the data retrieved from the stored procedures.
    //     I had on my SQL database, giving a input parameter
    //     sqlParam as you can see before
    //     Create datasource to the report - this way you associate
    //     these local datasets to your project datasets,
    //     with the correctly loaded data
    //     Notice that DS1_stp_s_LoadHeaderData and
    //     DS1_stp_s_LoadDetailData are my Report DataSets,
    //     so you can Map the local Datasets to the report datasets

    ReportDataSource reportDSHeader = 
      new ReportDataSource("DS1_stp_s_LoadHeaderData", 
                           dsHeader.Tables[0]);
    ReportDataSource reportDSDetail = 
      new ReportDataSource("DS1_stp_s_LoadDetailData", 
                           dsDetail.Tables[0]);

    //  6. Add these DataSources to your ReportViewer, et voilá!

    ReportViewer1.LocalReport.DataSources.Add(reportDSHeader);
    ReportViewer1.LocalReport.DataSources.Add(reportDSDetail);

    ReportViewer1.LocalReport.Refresh();
}

然后,您只需要调用 LoadReport 方法并传递正确的参数,就完成了。

关注点

这样,您就可以避免使用 Reporting Services 服务器,并节省大量不必要的代码。您的报表将与 Web 应用程序一起,因此一旦部署项目,您的报表就已经设置好了,无需将其安装到任何服务器上。

您只需要安装 .NET Framework 2.0 和 VS 2005 即可开发/运行此代码。

© . All rights reserved.