使用 WebForms ReportViewer 控制項
若要檢視已部署至報表伺服器或存在於本機檔案系統的報表,您可以使用 WebForms ReportViewer 控制項在 Web 應用程式中加以轉譯。
若要將 ReportViewer 控制項加入 Web 應用程式
使用 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 產品範例 (英文)。
此範例使用整合的 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 產品範例專案中的 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