WebForms ReportViewer 컨트롤 사용
보고서 서버에 배포된 보고서 또는 로컬 파일 시스템에 있는 보고서를 보려면 WebForms ReportViewer 컨트롤을 사용하여 웹 응용 프로그램에서 렌더링할 수 있습니다.
ReportViewer 컨트롤을 웹 응용 프로그램에 추가하려면
MicrosoftVisual C# 또는 MicrosoftVisual Basic을 사용하여 새 Microsoft ASP.NET 웹 사이트를 만듭니다.
- 또는 -
기존 ASP.NET 웹 사이트를 열고 새 Web Form을 추가합니다.
도구 상자에서 ReportViewer 컨트롤을 찾습니다. 도구 상자가 보이지 않을 경우 보기 메뉴에서 도구 상자를 선택하여 액세스할 수 있습니다.
ReportViewer 컨트롤을 Web Form의 디자인 화면으로 끌어 놓습니다.
이름이 reportViewer1인 ReportViewer 컨트롤이 폼에 추가됩니다.
컨트롤이 폼에 추가되면 ReportViewer 태스크 스마트 태그가 나타나 보고서를 선택하도록 알립니다. 보려는 보고서가 보고서 서버에 배포된 경우 보고서 선택 드롭다운 목록에서 <서버 보고서> 옵션을 선택합니다. <서버 보고서> 옵션이 선택되면 보고서 서버 URL 및 보고서 경로의 두 가지 속성이 추가로 나타납니다. 보고서 서버 URL은 보고서 서버에 대한 주소이고 보고서 경로는 렌더링하려는 보고서의 전체 경로입니다. 로컬 모드로 보고서를 보려는 경우 새 보고서 디자인 옵션을 선택하여 보고서 디자이너를 시작하거나 기존 프로젝트에 속한 보고서를 선택합니다.
원격 처리 모드로 보고서 보기
다음 예는 보고서 서버에 배포된 보고서를 렌더링하는 방법을 보여 줍니다. 이 예에서는 AdventureWorks 예제 보고서 프로젝트에 포함된 Sales Order Detail 보고서를 사용합니다. 이 예제 보고서를 배포하는 방법은 SQL Server Reporting Services 제품 예제(SQL Server Reporting Services Product Samples)를 참조하십시오.
이 예에서는 Windows 통합 인증을 사용하므로 먼저 가장을 사용하도록 설정해야 합니다. 그러려면 다음 줄을 web.config 파일에 삽입합니다.
<!-- Web.config file. -->
<identity impersonate="true"/>
[!참고]
가장은 기본적으로 사용 안 함으로 설정되어 있습니다.
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 });
}
}
Imports Microsoft.Reporting.WebForms
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Init
If Not Page.IsPostBack Then
'Set the processing mode for the ReportViewer to Remote
reportViewer.ProcessingMode = ProcessingMode.Remote
Dim serverReport As 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
Dim salesOrderNumber As New ReportParameter()
salesOrderNumber.Name = "SalesOrderNumber"
salesOrderNumber.Values.Add("SO43661")
'Set the report parameters for the report
Dim parameters() As ReportParameter = {salesOrderNumber}
serverReport.SetParameters(parameters)
End If
End Sub
End Class
로컬 처리 모드로 보고서 보기
다음 예는 Windows 응용 프로그램의 일부이고 보고서 서버에 배포되지 않은 보고서를 렌더링하는 방법을 보여 줍니다. 이 예에서도 SQL Server Reporting Services 제품 예제(SQL Server Reporting Services Product Samples) 프로젝트의 Sales Order Detail 보고서를 사용합니다.
Sales Order Detail 보고서를 웹 사이트에 추가하려면
보고서를 추가할 웹 사이트를 엽니다.
웹 사이트 메뉴에서 기존 항목 추가를 선택합니다.
AdventureWorks 보고서 예제 프로젝트가 설치된 위치를 찾습니다.
기본 위치는 C:\Program Files\Microsoft SQL Server\100\Samples\Reporting Services\Report Samples\AdventureWorks Sample Reports입니다. 예제를 설치하는 방법은 SQL Server 예제 및 예제 데이터베이스 설치 시 고려 사항를 참조하십시오.
Sales Order Detail.rdl 파일을 선택하고 추가 단추를 클릭합니다.
이제 Sales Order Detail.rdl 파일이 프로젝트의 일부여야 합니다.
솔루션 탐색기에서 Sales Order Detail.rdl 파일을 마우스 오른쪽 단추로 클릭하고 이름 바꾸기를 선택합니다. 보고서 이름을 Sales Order Detail.rdlc로 바꾸고 Enter 키를 누릅니다.
솔루션 탐색기가 보이지 않으면 보기 메뉴에서 솔루션 탐색기를 선택하여 열 수 있습니다.
다음 코드 예에서는 판매 주문 데이터에 대한 데이터 집합을 만든 다음 Sales Order Detail 보고서를 로컬 모드에서 렌더링합니다.
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");
}
}
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.Reporting.WebForms
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Init
If Not Page.IsPostBack Then
'Set the processing mode for the ReportViewer to Local
reportViewer.ProcessingMode = ProcessingMode.Local
Dim localReport As LocalReport
localReport = reportViewer.LocalReport
localReport.ReportPath = "Sales Order Detail.rdlc"
Dim dataset As New DataSet("Sales Order Detail")
Dim salesOrderNumber As String = "SO43661"
'Get the sales order data
GetSalesOrderData(salesOrderNumber, dataset)
'Create a report data source for the sales order data
Dim dsSalesOrder As New ReportDataSource()
dsSalesOrder.Name = "SalesOrder"
dsSalesOrder.Value = dataset.Tables("SalesOrder")
localReport.DataSources.Add(dsSalesOrder)
'Get the sales order detail data
GetSalesOrderDetailData(salesOrderNumber, dataset)
'Create a report data source for the sales
'order detail data
Dim dsSalesOrderDetail As New ReportDataSource()
dsSalesOrderDetail.Name = "SalesOrderDetail"
dsSalesOrderDetail.Value = _
dataset.Tables("SalesOrderDetail")
localReport.DataSources.Add(dsSalesOrderDetail)
'Create a report parameter for the sales order number
Dim rpSalesOrderNumber As New ReportParameter()
rpSalesOrderNumber.Name = "SalesOrderNumber"
rpSalesOrderNumber.Values.Add("SO43661")
'Set the report parameters for the report
Dim parameters() As ReportParameter = {rpSalesOrderNumber}
localReport.SetParameters(parameters)
End If
End Sub
Private Sub GetSalesOrderData(ByVal salesOrderNumber As String, _
ByRef dsSalesOrder As DataSet)
Dim sqlSalesOrder As String = _
"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)"
Using connection As New SqlConnection( _
"Data Source=(local); " & _
"Initial Catalog=AdventureWorks; " & _
"Integrated Security=SSPI")
Dim command As New SqlCommand(sqlSalesOrder, connection)
Dim parameter As New SqlParameter("SalesOrderNumber", _
salesOrderNumber)
command.Parameters.Add(parameter)
Dim salesOrderAdapter As New SqlDataAdapter(command)
salesOrderAdapter.Fill(dsSalesOrder, "SalesOrder")
End Using
End Sub
Private Sub GetSalesOrderDetailData( _
ByVal salesOrderNumber As String, _
ByRef dsSalesOrder As DataSet)
Dim sqlSalesOrderDetail As String = _
"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 connection As New SqlConnection( _
"Data Source=(local); " & _
"Initial Catalog=AdventureWorks; " & _
"Integrated Security=SSPI")
Dim command As New SqlCommand(sqlSalesOrderDetail, _
connection)
Dim parameter As New SqlParameter("SalesOrderNumber", _
salesOrderNumber)
command.Parameters.Add(parameter)
Dim salesOrderDetailAdapter As New SqlDataAdapter(command)
salesOrderDetailAdapter.Fill(dsSalesOrder, _
"SalesOrderDetail")
End Using
End Sub
End Class