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

在本地模式下使用 ASP.NET 2.0 ReportViewer

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.79/5 (67投票s)

2006年9月14日

CPOL

6分钟阅读

viewsIcon

2061966

downloadIcon

1

如何使用 ASP.NET 2.0 ReportViewer 控件在本地模式下工作。

引言

网上有很多关于“SQL Reporting Services 服务器模式”的资料,但我花了一段时间才研究清楚“本地模式”,尤其是在涉及参数的情况下。

选择“本地模式”而非“服务器模式”的原因是,在“服务器模式”下,客户端向服务器发出报表请求。服务器生成报表,然后将其发送给客户端。虽然这更安全,但大型报表会因为从服务器到浏览器的传输时间而降低性能。在“本地模式”下,报表在客户端生成。本地模式不需要连接到“SQL Server Reporting Services 服务器”。大型报表不会增加等待时间。

因此,这是一篇关于如何使用 ASP.NET 2.0 ReportViewer Web 服务器控件在本地模式下通过带参数的存储过程生成报表的文章。我使用的是 ASP.NET 2.0、Visual Studio 2005 和 SQL Server 2005 及应用程序块。如果您不使用 Microsoft 应用程序块,只需通过 SQL Command 对象调用存储过程,而无需使用示例中的 SQL Helper 类。

使用 Northwind 数据库,我们的示例将提示用户从下拉列表中选择一个类别,然后显示该类别下的所有产品。

步骤 1:创建带参数的存储过程

ALTER PROCEDURE  ShowProductByCategory(@CategoryName nvarchar(15) )
AS
SELECT  Categories.CategoryName, Products.ProductName, 
        Products.UnitPrice, Products.UnitsInStock
FROM    Categories INNER JOIN Products ON 
        Categories.CategoryID = Products.CategoryID
WHERE   CategoryName=@CategoryName
RETURN

步骤 2:使用 DataSet Designer 在类型化的 DataSet 中创建 DataTable

在解决方案资源管理器中,右键单击 App_Code 文件夹。选择“添加新项”。选择“DataSet”。为您的数据集命名,例如 DataSetProducts.xsd,然后单击“添加”。TableAdapter 配置向导应该会自动出现,如果没有,请右键单击 DataSet Designer 屏幕上的任意位置,然后从上下文菜单中选择“添加”。选择“TableAdapter”以调出向导。按照向导创建您的数据表。我选择了“使用现有的存储过程”作为命令类型,并指定“ShowProductByCategory”作为 Select 命令。我还将“CategoryName”突出显示为 Select 过程参数。

步骤 1 中创建的存储过程的结果最终将放入步骤 2 中创建的此数据表中(图 1)。报表数据通过数据表提供。

图 1 DataSetProducts.xsd 包含一个 DataTable,用作报表数据源。

步骤 3:创建报表定义

在解决方案资源管理器中,右键单击并选择“添加新项”。选择“Report”模板。在此示例中,我将使用默认名称 Report.rdlc。单击“添加”将 Report.rdlc 添加到您的项目中。“rdl”代表报表定义语言。“c”代表客户端。因此,扩展名 .rdlc 代表本地报表。

从工具箱将“Table”拖到报表设计器屏幕上(图 2)。此处显示的工具箱特定于报表模板。它显示了要在报表中使用的控件,而不是要在 Web 窗体中使用的控件。“Table”有三个区域:页眉、详细信息和页脚。

Table”是数据区域。数据区域用于显示来自底层数据集的数据绑定报表项。虽然报表可以有多个数据区域,但每个数据区域只能显示来自一个 DataSet 的数据。因此,使用存储过程将多个表链接到单个 DataSet 以供报表使用。

图 2 工具箱包含特定于报表模板的控件。

打开“网站数据源”窗口(图 3)。找到“DataSetProductsDataSet(在步骤 2 中创建)。展开以查看 DataTableShowProductByCategory”中的列。该表名为“ShowProductByCategory”,因为我们在 TableAdapter 配置向导中选择了“使用现有的存储过程”。并且我们的过程名称是“ShowProductByCategory”。

将“ProductName”列从“网站数据源”窗口拖到详细信息行(中间行)。将“UnitPrice”拖到第二个详细信息列,将“UnitsInStock”拖到最后一列。页眉会自动显示。您可以右键单击详细信息行中的任何字段(例如,右键单击“Unit Price”)并调出上下文菜单。从上下文菜单中选择“属性”。选择“格式”选项卡以相应地格式化“Unit Price”和“Units In Stock”。

图 3。网站数据源窗口显示您应用程序中的类型化数据集及其列。

步骤 4:将 ReportViewer Web 服务器控件拖到一个 .aspx 窗体上

DropDownList 控件拖到一个新的 Web 窗体上(图 4)。使用“DropDownList 任务”中的“选择数据源”选项,从 Category 表绑定 CategoryName 字段。请记住启用自动回发。然后,用户可以选择作为存储过程的输入。虽然在此示例中我使用的是 DropDownList,但您也可以使用文本框和其他控件来提示用户进行额外输入。

ReportViewer Web 服务器控件拖到 Web 窗体上。将其 Visible 属性设置为 false。另外请注意,ASP.NET 2.0 中的 ReportViewer Web 服务器控件提供了导出功能。您可以选择 Excel 格式或 PDF 格式。但是,我发现屏幕上看到的内容并不总是与打印机输出的内容一致。您需要进一步试验输出格式。

图 4 将此网页设置为启动页。

接下来,调出 ReportViewer 控件的智能标记(图 5)。在“选择报表”下拉列表中选择“Report.rdlc”。“Report.rdlc”是在步骤 3 中创建的。本地报表具有 .rdlc 扩展名。服务器报表标记为 .rdc

图 5 将报表定义文件(.rdlc)关联到 ReportViewer 控件

步骤 5:编写“运行报表”按钮的源代码,以根据用户选择生成报表

不要忘记在代码隐藏文件中包含“Microsoft.Reporting.WebForms”命名空间。

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.ApplicationBlocks.Data;
using Microsoft.Reporting.WebForms;

public partial class ReportViewerLocalMode : System.Web.UI.Page
{
    public string thisConnectionString = 
       ConfigurationManager.ConnectionStrings[
       "NorthwindConnectionString"].ConnectionString;

    /*I used the following statement to show if you have multiple 
      input parameters, declare the parameter with the number 
      of parameters in your application, ex. New SqlParameter[4]; */

    public SqlParameter[] SearchValue = new SqlParameter[1];

    protected void RunReportButton_Click(object sender, EventArgs e)
    {
        //ReportViewer1.Visible is set to false in design mode
        ReportViewer1.Visible = true;
        SqlConnection thisConnection = new SqlConnection(thisConnectionString);
        System.Data.DataSet thisDataSet = new System.Data.DataSet();       
        SearchValue[0] = new SqlParameter("@CategoryName", 
                         DropDownList1.SelectedValue);

        /* Put the stored procedure result into a dataset */
        thisDataSet = SqlHelper.ExecuteDataset(thisConnection, 
                      "ShowProductByCategory", SearchValue);

        /*or   thisDataSet = SqlHelper.ExecuteDataset(thisConnection, 
               "ShowProductByCategory", dropdownlist1.selectedvalue); 
               if you only have 1 input parameter  */

        /* Associate thisDataSet  (now loaded with the stored 
           procedure result) with the  ReportViewer datasource */
        ReportDataSource datasource = new 
          ReportDataSource("DataSetProducts_ShowProductByCategory", 
          thisDataSet.Tables[0]);

        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(datasource);
        if (thisDataSet.Tables[0].Rows.Count == 0)
        {
            lblMessage.Text = "Sorry, no products under this category!";
        }

        ReportViewer1.LocalReport.Refresh();
    }
}

步骤 6:构建并运行报表

按 F5 运行 .aspx 文件。单击“运行报表”按钮,根据下拉列表中选择的类别查看产品列表(图 6)。

图 6 单击“运行报表”按钮生成本地报表

请确保将 ReportViewer 的引用添加到您的 Web 应用程序中,并注意您的 ReportViewer Web 服务器控件已在 web.config 文件中注册了一个 HTTP 处理程序。您的 web.config 文件应包含以下字符串:

<httpHandlers>
    <add path="Reserved.ReportViewerWebControl.axd" verb="*" 
         type="Microsoft.Reporting.WebForms.HttpHandler, 
               Microsoft.ReportViewer.WebForms, 
               Version=8.0.0.0, Culture=neutral, 
               PublicKeyToken=?????????????"
         validate="false" />
</httpHandlers>

当您在网站中使用 Visual Studio 2005 ReportViewer Web 服务器控件时,您需要将“C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages\ReportViewer\ReportViewer.exe”复制到您的服务器,并在部署带有 ReportViewer 控件的 Web 页面之前运行它。

好了,就是这样。这是一个创建本地模式报表的简单示例。希望您觉得这个例子有用。祝您计算愉快!

© . All rights reserved.