[转]使用 WebForms ReportViewer 控件

正文转自:http://technet.microsoft.com/zh-cn/library/aa337091

 

若要查看安插到报表服务器的表格或在地面文件系统上设有的报表,您能够在 Web
应用程序中使用 WebForms ReportViewer 控件来展现它们。

 

 

 

 

 

 

将 ReportViewer 控件添加到 Web 应用程序

  1. 使用
    Microsoft Visual C# 或 Microsoft Visual Basic 创造二个新的
    Microsoft ASP.NET 网站。


    或 –

    开辟二个共处的
    ASP.NET 网站并加上二个新的 Web 窗体。

  2. 在“工具箱”中找到 ReportViewer 控件。 假诺“工具箱”不可见,则足以经过从“视图”菜单中选取“工具箱”来进行访问。


  3. ReportViewer 控件拖到 Web 窗体的规划图面上。

    名为
    reportViewer1 的 ReportViewer 控件便添加到该窗体中。

将控件添加到该窗体之后,“ReportViewer
职务”智能标记将出现,提醒您选拔某一报表。 倘若你要翻开的表格已布局到报表服务器上,则从“选用报表”下拉列表中甄选<服务器报表>选项。 接纳<服务器报表>选项后,将现出四个叠加的性质:“报表服务器 UQX56L”和“报表路径”。 “报表服务器 UEvoqueL”是指向相应报表服务器的地址,“报表路径”是指向要展现的报表的完全路径。
假若您想要以当地形式查看报表,则采取“设计新报表”选项以运营报表设计器或选拔已是现有项目的一有个其余表格。

在长途处理格局下查看报表)


上边包车型大巴示范演示如何表现已铺排到某一报表服务器的报表。
此示例使用     AdventureWorks二零一二  
示例报表项目随附的 Sales Order Detail 报表。

该示例使用集成的
Windows 身份验证,由此你首先必须启用模拟。 为此,将以下行插入
web.config 文件:

 

复制window.epx.codeSnippet.copyCode(‘CodeSnippetContainerCode_0ff6de4d-61a7-4a1b-a1a8-dc6e9b1ce19d’);)

<!-- Web.config file. -->
<identity impersonate="true"/>
注意

默认情况下,模拟处于禁用状态。

C#

VB

复制window.epx.codeSnippet.copyCode(‘CodeSnippetContainerCode_2aa292be-4209-49db-8bcb-ecc34256c22b’);)

protected void Page_Init(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // Set the processing mode for the ReportViewer to Remote
        reportViewer.ProcessingMode = ProcessingMode.Remote;

        ServerReport serverReport = reportViewer.ServerReport;

        // Set the report server URL and report path
        serverReport.ReportServerUrl =
            new Uri("http://<Server Name>/reportserver");
        serverReport.ReportPath =
            "/AdventureWorks Sample Reports/Sales Order Detail";

        // Create the sales order number report parameter
        ReportParameter salesOrderNumber = new ReportParameter();
        salesOrderNumber.Name = "SalesOrderNumber";
        salesOrderNumber.Values.Add("SO43661");

        // Set the report parameters for the report
        reportViewer.ServerReport.SetParameters(
            new ReportParameter[] { salesOrderNumber });
    }
}

在本土处理情势下查看报表)


上面包车型大巴示范演示如何展现作为
Windows 应用程序的一有的还要没有安顿到报表服务器的表格。

 

 

 

 

 

 

将 Sales Order Detail 报表添加到网站

  1. 开拓该报表将添加到的网站。

  2. 在“网站”菜单中,选用“添加现有项”。

  3. 浏览到安装了
    AdventureWorks 报表示例项目标地点。

    暗中认可地方是
    C:\Program Files\Microsoft SQL Server\100\Samples\Reporting
    Services\Report Samples\AdventureWorks Sample Reports。

  4. 选择Sales Order Detail.rdl 文件并单击“添加”按钮。

    Sales
    Order Detail.rdl 文件未来应是该品种的一部分。

  5. 在缓解方案能源管理器中右键单击
    Sales Order Detail.rdl 文件,然后选择“重命名”。 将该报表重命名为
    Sales Order Detail.rdlc,然后按 Enter。

    假如消除方案财富管理器不可知,则能够通过从“视图”菜单中选用“消除方案财富管理器”来将其开辟。

 

以下代码示例将为销售订单数量创制3个数据集,然后以本土格局表现
Sales Order Detail 报表。

C#

VB

复制window.epx.codeSnippet.copyCode(‘CodeSnippetContainerCode_5b2d9d9f-d223-4308-a60b-eff4256eeade’);)

protected void Page_Init(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // Set the processing mode for the ReportViewer to Local
        reportViewer.ProcessingMode = ProcessingMode.Local;

        LocalReport localReport = reportViewer.LocalReport;

        localReport.ReportPath = "Sales Order Detail.rdlc";

        DataSet dataset = new DataSet("Sales Order Detail");

        string salesOrderNumber = "SO43661";

        GetSalesOrderData(salesOrderNumber, ref dataset);

        ReportDataSource dsSalesOrder = new ReportDataSource();
        dsSalesOrder.Name = "SalesOrder";
        dsSalesOrder.Value = dataset.Tables["SalesOrder"];

        localReport.DataSources.Add(dsSalesOrder);

        GetSalesOrderDetailData(salesOrderNumber, ref dataset);

        ReportDataSource dsSalesOrderDetail = new ReportDataSource();
        dsSalesOrderDetail.Name = "SalesOrderDetail";
        dsSalesOrderDetail.Value = dataset.Tables["SalesOrderDetail"];

        localReport.DataSources.Add(dsSalesOrderDetail);

        // Create the sales order number report parameter
        ReportParameter rpSalesOrderNumber = new ReportParameter();
        rpSalesOrderNumber.Name = "SalesOrderNumber";
        rpSalesOrderNumber.Values.Add("SO43661");

        // Set the report parameters for the report
        localReport.SetParameters(
            new ReportParameter[] { rpSalesOrderNumber });
    }
}

private void GetSalesOrderData(string salesOrderNumber,
                           ref DataSet dsSalesOrder)
{
    string sqlSalesOrder =
        "SELECT SOH.SalesOrderNumber, S.Name AS Store, " +
        "       SOH.OrderDate, C.FirstName AS SalesFirstName, " +
        "       C.LastName AS SalesLastName, E.Title AS " +
        "       SalesTitle, SOH.PurchaseOrderNumber, " +
        "       SM.Name AS ShipMethod, BA.AddressLine1 " +
        "       AS BillAddress1, BA.AddressLine2 AS " +
        "       BillAddress2, BA.City AS BillCity, " +
        "       BA.PostalCode AS BillPostalCode, BSP.Name " +
        "       AS BillStateProvince, BCR.Name AS " +
        "       BillCountryRegion, SA.AddressLine1 AS " +
        "       ShipAddress1, SA.AddressLine2 AS " +
        "       ShipAddress2, SA.City AS ShipCity, " +
        "       SA.PostalCode AS ShipPostalCode, SSP.Name " +
        "       AS ShipStateProvince, SCR.Name AS " +
        "       ShipCountryRegion, CC.Phone AS CustPhone, " +
        "       CC.FirstName AS CustFirstName, CC.LastName " +
        "       AS CustLastName " +
        "FROM   Person.Address SA INNER JOIN " +
        "       Person.StateProvince SSP ON " +
        "       SA.StateProvinceID = SSP.StateProvinceID " +
        "       INNER JOIN Person.CountryRegion SCR ON " +
        "       SSP.CountryRegionCode = SCR.CountryRegionCode " +
        "       RIGHT OUTER JOIN Sales.SalesOrderHeader SOH " +
        "       LEFT OUTER JOIN  Person.Contact CC ON " +
        "       SOH.ContactID = CC.ContactID LEFT OUTER JOIN" +
        "       Person.Address BA INNER JOIN " +
        "       Person.StateProvince BSP ON " +
        "       BA.StateProvinceID = BSP.StateProvinceID " +
        "       INNER JOIN Person.CountryRegion BCR ON " +
        "       BSP.CountryRegionCode = " +
        "       BCR.CountryRegionCode ON SOH.BillToAddressID " +
        "       = BA.AddressID ON  SA.AddressID = " +
        "       SOH.ShipToAddressID LEFT OUTER JOIN " +
        "       Person.Contact C RIGHT OUTER JOIN " +
        "       HumanResources.Employee E ON C.ContactID = " +
        "       E.ContactID ON SOH.SalesPersonID = " +
        "       E.EmployeeID LEFT OUTER JOIN " +
        "       Purchasing.ShipMethod SM ON SOH.ShipMethodID " +
        "       = SM.ShipMethodID LEFT OUTER JOIN Sales.Store" +
        "        S ON SOH.CustomerID = S.CustomerID " +
        "WHERE  (SOH.SalesOrderNumber = @SalesOrderNumber)";

    SqlConnection connection = new
        SqlConnection("Data Source=(local); " +
                      "Initial Catalog=AdventureWorks; " +
                      "Integrated Security=SSPI");

    SqlCommand command =
        new SqlCommand(sqlSalesOrder, connection);

    command.Parameters.Add(
        new SqlParameter("SalesOrderNumber",
        salesOrderNumber));

    SqlDataAdapter salesOrderAdapter = new
        SqlDataAdapter(command);

    salesOrderAdapter.Fill(dsSalesOrder, "SalesOrder");
}

private void GetSalesOrderDetailData(string salesOrderNumber,
                       ref DataSet dsSalesOrder)
{
    string sqlSalesOrderDetail =
        "SELECT  SOD.SalesOrderDetailID, SOD.OrderQty, " +
        "        SOD.UnitPrice, CASE WHEN " +
        "        SOD.UnitPriceDiscount IS NULL THEN 0 " +
        "        ELSE SOD.UnitPriceDiscount END AS " +
        "        UnitPriceDiscount, SOD.LineTotal, " +
        "        SOD.CarrierTrackingNumber, " +
        "        SOD.SalesOrderID, P.Name, P.ProductNumber " +
        "FROM    Sales.SalesOrderDetail SOD INNER JOIN " +
        "        Production.Product P ON SOD.ProductID = " +
        "        P.ProductID INNER JOIN " +
        "        Sales.SalesOrderHeader SOH ON " +
        "        SOD.SalesOrderID = SOH.SalesOrderID " +
        "WHERE   (SOH.SalesOrderNumber = @SalesOrderNumber) " +
        "ORDER BY SOD.SalesOrderDetailID";

    using (SqlConnection connection = new
        SqlConnection("Data Source=(local); " +
                      "Initial Catalog=AdventureWorks; " +
                      "Integrated Security=SSPI"))
    {

        SqlCommand command =
            new SqlCommand(sqlSalesOrderDetail, connection);

        command.Parameters.Add(
            new SqlParameter("SalesOrderNumber",
            salesOrderNumber));

        SqlDataAdapter salesOrderDetailAdapter = new
            SqlDataAdapter(command);

        salesOrderDetailAdapter.Fill(dsSalesOrder,
            "SalesOrderDetail");
    }
}

请参阅)


概念

选用 ReportViewer 控件集成 Reporting
Services

相关文章