VS2005+SQL2005 Reporting Service动态绑定报表(Web)[转]

(这篇文章让我解决了头痛的问题,所以收藏一下,谢谢Carlwave-陆飞(Fei.Lu)的好文章)
一、综述

        这里我要介绍的是如何使用 VS2005 中的 ReportViewer 控件动态显示需要绑定的报表( *.rdlc )。由于我们已经很习惯使用 DataSet 作为数据源去绑定类似于 GridView 或者 DataGrid 等控件,那么是否有方法将 DataSet 作为数据源就像绑定 GridView 这样去绑定报表呢?答案肯定的。

        让我们先看下最简单的连接方法Reporting Service使用dataset.xsd作为数据源,建立一个页面page.aspx对应一个reportviewer控件对应一张report.rdlc报表对应一个dataset.xsd数据源。运用这种模式,那么在VS2005中提供了一个强大的向导,只需按照向导操作,网上资料或者MSDN中都介绍的很详细如何使用这个向导,在此不再详细描述。

        其次,也是我主要要介绍的,动态绑定到ado.net dataset:由于大部分数据都是采用ado.net dataset作为数据源绑定,如果使用提供的向导操作便不能进行动态绑定,对于后台操作非常不便,故不能使用向导操作。现在要设法动态绑定ado.net dataset到指定的report。
        优点在于代码简单,操作灵活;
        缺点在于设计report文件将变得非常令人头疼。
        report文件是xml格式的,如果不使用向导那么就必须人为的去写这个xml文件(我研究了很久,没找到其他设计报表的方法,这麽写也是摸索出来的,掌握了就很方便了),特别当存储过程是动态的时候,设计这个报表的数据源将变得更为烦锁。但是同样的优点在于一切的问题只要掌握了如何去写这个文件,那么就等于解决了所有问题,独立性强。

二、使用WebForms ReportViewer控件

        在一个WEB应用程序中,为了浏览在服务器上(report server)配置好的报表或者在本地文件系统(local file system)上的报表,你可以使用WebForms ReportViewer控件。

增加一个ReportViewer控件到一个Web应用程序

1、 增加一个新的Microsoft ASP.NET Web Site使用Microsoft Visual C# 或者 Microsoft Visual Basic。

2、 从工具箱中将ReportViewer控件拖到设计页面上,命名为reportViewer,当增加完之后,ReportViewer任务标签中会出现提示让你选择一个报表,此处可不去管它。

3、使用远程处理模式(Remote Processing Mode)浏览报表

下面的例子详细展示了如何提交一个报表存在于报表服务器上,这个例子使用Sales Order Detail Report。这张表报存在于SQL2005的示例(AdventureWorks)中,你只需安装SQL2005的示例,便能从里面找到,更详细信息关于示例,请看AdventureWorks Report Samples

       这个例子使用Windows综合认证,所以你必须首先在web.config中加入

<!-- Web.config file. --> <identity impersonate="true"/>
C#

 1 None.gif protected   void  Page_Init( object  sender, EventArgs e)
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif    if (!Page.IsPostBack)
 4ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 5InBlock.gif        // Set the processing mode for the ReportViewer to Remote
 6InBlock.gif        reportViewer.ProcessingMode = ProcessingMode.Remote;
 7InBlock.gif
 8InBlock.gif        ServerReport serverReport = reportViewer.ServerReport;
 9InBlock.gif
10InBlock.gif        // Set the report server URL and report path
11InBlock.gif        serverReport.ReportServerUrl =
12InBlock.gif            new Uri("http://localhost/reportserver");
13InBlock.gif        serverReport.ReportPath =
14InBlock.gif            "/AdventureWorks Sample Reports/Sales Order Detail";
15InBlock.gif
16InBlock.gif        // Create the sales order number report parameter
17InBlock.gif        ReportParameter salesOrderNumber = new ReportParameter();
18InBlock.gif        salesOrderNumber.Name = "SalesOrderNumber";
19InBlock.gif        salesOrderNumber.Values.Add("SO43661");
20InBlock.gif
21InBlock.gif        // Set the report parameters for the report
22InBlock.gif        reportViewer.ServerReport.SetParameters(
23ExpandedSubBlockStart.gifContractedSubBlock.gif            new ReportParameter[] dot.gif{ salesOrderNumber });
24ExpandedSubBlockEnd.gif    }

25ExpandedBlockEnd.gif}

 VB

 1 None.gif Imports  Microsoft.Reporting.WebForms
 2 None.gif
 3 ExpandedBlockStart.gifContractedBlock.gifPartial  Class _Default Class _Default
 4InBlock.gif    Inherits System.Web.UI.Page
 5InBlock.gif
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    Protected Sub Page_Init()Sub Page_Init(ByVal sender As Object, _
 7InBlock.gif            ByVal e As System.EventArgs) Handles Me.Init
 8InBlock.gif
 9InBlock.gif        If Not Page.IsPostBack Then
10InBlock.gif
11InBlock.gif            'Set the processing mode for the ReportViewer to Remote
12InBlock.gif            reportViewer.ProcessingMode = ProcessingMode.Remote
13InBlock.gif
14InBlock.gif            Dim serverReport As ServerReport
15InBlock.gif            serverReport = reportViewer.ServerReport
16InBlock.gif
17InBlock.gif            'Set the report server URL and report path
18InBlock.gif            serverReport.ReportServerUrl = _
19InBlock.gif                New Uri("http://localhost/reportserver")
20InBlock.gif            serverReport.ReportPath = _
21InBlock.gif                "/AdventureWorks Sample Reports/Sales Order Detail"
22InBlock.gif
23InBlock.gif            'Create the sales order number report parameter
24InBlock.gif            Dim salesOrderNumber As New ReportParameter()
25InBlock.gif            salesOrderNumber.Name = "SalesOrderNumber"
26InBlock.gif            salesOrderNumber.Values.Add("SO43661")
27InBlock.gif
28InBlock.gif            'Set the report parameters for the report
29InBlock.gif            Dim parameters() As ReportParameter = {salesOrderNumber}
30InBlock.gif            serverReport.SetParameters(parameters)
31InBlock.gif
32InBlock.gif        End If
33InBlock.gif
34ExpandedSubBlockEnd.gif    End Sub

35InBlock.gif
36ExpandedBlockEnd.gifEnd Class

 

4、使用本地处理模式(Local Processing Mode)浏览报表

这个例子同样使用了AdventureWorks Report Samples,你可以在本文章的最下面点击下载我做好的例子。

(1)       打开一个Web Site提供报表的增加

(2)       从菜单栏选择增加现有项

(3)       将C:\Program Files\Microsoft SQL Server\90\Samples\Reporting Services\Report Samples\AdventureWorks Sample Reports\ Sales Order Detail.rdl 报表文件加入项目

(4)       重命名Sales Order Detail.rdl为Sales Order Detail.rdlc

(5)       下面的例子将会在本地模式下建立一个dataset作为报表的数据源显示报表

 C#

  1 None.gif protected   void  Page_Init( object  sender, EventArgs e)
  2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  3InBlock.gif    if (!Page.IsPostBack)
  4ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  5InBlock.gif        // Set the processing mode for the ReportViewer to Local
  6InBlock.gif        reportViewer.ProcessingMode = ProcessingMode.Local;
  7InBlock.gif
  8InBlock.gif        LocalReport localReport = reportViewer.LocalReport;
  9InBlock.gif
 10InBlock.gif        localReport.ReportPath = "Sales Order Detail.rdlc";
 11InBlock.gif
 12InBlock.gif        DataSet dataset = new DataSet("Sales Order Detail");
 13InBlock.gif
 14InBlock.gif        string salesOrderNumber = "SO43661";
 15InBlock.gif
 16InBlock.gif        GetSalesOrderData(salesOrderNumber, ref dataset);
 17InBlock.gif
 18InBlock.gif        ReportDataSource dsSalesOrder = new ReportDataSource();
 19InBlock.gif        dsSalesOrder.Name = "SalesOrder";
 20InBlock.gif        dsSalesOrder.Value = dataset.Tables["SalesOrder"];
 21InBlock.gif
 22InBlock.gif        localReport.DataSources.Add(dsSalesOrder);
 23InBlock.gif
 24InBlock.gif        GetSalesOrderDetailData(salesOrderNumber, ref dataset);
 25InBlock.gif
 26InBlock.gif        ReportDataSource dsSalesOrderDetail = new ReportDataSource();
 27InBlock.gif        dsSalesOrderDetail.Name = "SalesOrderDetail";
 28InBlock.gif        dsSalesOrderDetail.Value = dataset.Tables["SalesOrderDetail"];
 29InBlock.gif
 30InBlock.gif        localReport.DataSources.Add(dsSalesOrderDetail);
 31InBlock.gif
 32InBlock.gif        // Create the sales order number report parameter
 33InBlock.gif        ReportParameter rpSalesOrderNumber = new ReportParameter();
 34InBlock.gif        rpSalesOrderNumber.Name = "SalesOrderNumber";
 35InBlock.gif        rpSalesOrderNumber.Values.Add("SO43661");
 36InBlock.gif
 37InBlock.gif        // Set the report parameters for the report
 38InBlock.gif        localReport.SetParameters(
 39ExpandedSubBlockStart.gifContractedSubBlock.gif            new ReportParameter[] dot.gif{ rpSalesOrderNumber });
 40ExpandedSubBlockEnd.gif    }

 41ExpandedBlockEnd.gif}

 42 None.gif
 43 None.gif private   void  GetSalesOrderData( string  salesOrderNumber,
 44 None.gif                            ref  DataSet dsSalesOrder)
 45 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 46InBlock.gif    string sqlSalesOrder =
 47InBlock.gif        "SELECT SOH.SalesOrderNumber, S.Name AS Store, " +
 48InBlock.gif        "       SOH.OrderDate, C.FirstName AS SalesFirstName, " +
 49InBlock.gif        "       C.LastName AS SalesLastName, E.Title AS " +
 50InBlock.gif        "       SalesTitle, SOH.PurchaseOrderNumber, " +
 51InBlock.gif        "       SM.Name AS ShipMethod, BA.AddressLine1 " +
 52InBlock.gif        "       AS BillAddress1, BA.AddressLine2 AS " +
 53InBlock.gif        "       BillAddress2, BA.City AS BillCity, " +
 54InBlock.gif        "       BA.PostalCode AS BillPostalCode, BSP.Name " +
 55InBlock.gif        "       AS BillStateProvince, BCR.Name AS " +
 56InBlock.gif        "       BillCountryRegion, SA.AddressLine1 AS " +
 57InBlock.gif        "       ShipAddress1, SA.AddressLine2 AS " +
 58InBlock.gif        "       ShipAddress2, SA.City AS ShipCity, " +
 59InBlock.gif        "       SA.PostalCode AS ShipPostalCode, SSP.Name " +
 60InBlock.gif        "       AS ShipStateProvince, SCR.Name AS " +
 61InBlock.gif        "       ShipCountryRegion, CC.Phone AS CustPhone, " +
 62InBlock.gif        "       CC.FirstName AS CustFirstName, CC.LastName " +
 63InBlock.gif        "       AS CustLastName " +
 64InBlock.gif        "FROM   Person.Address SA INNER JOIN " +
 65InBlock.gif        "       Person.StateProvince SSP ON " +
 66InBlock.gif        "       SA.StateProvinceID = SSP.StateProvinceID " +
 67InBlock.gif        "       INNER JOIN Person.CountryRegion SCR ON " +
 68InBlock.gif        "       SSP.CountryRegionCode = SCR.CountryRegionCode " +
 69InBlock.gif        "       RIGHT OUTER JOIN Sales.SalesOrderHeader SOH " +
 70InBlock.gif        "       LEFT OUTER JOIN  Person.Contact CC ON " +
 71InBlock.gif        "       SOH.ContactID = CC.ContactID LEFT OUTER JOIN" +
 72InBlock.gif        "       Person.Address BA INNER JOIN " +
 73InBlock.gif        "       Person.StateProvince BSP ON " +
 74InBlock.gif        "       BA.StateProvinceID = BSP.StateProvinceID " +
 75InBlock.gif        "       INNER JOIN Person.CountryRegion BCR ON " +
 76InBlock.gif        "       BSP.CountryRegionCode = " +
 77InBlock.gif        "       BCR.CountryRegionCode ON SOH.BillToAddressID " +
 78InBlock.gif        "       = BA.AddressID ON  SA.AddressID = " +
 79InBlock.gif        "       SOH.ShipToAddressID LEFT OUTER JOIN " +
 80InBlock.gif        "       Person.Contact C RIGHT OUTER JOIN " +
 81InBlock.gif        "       HumanResources.Employee E ON C.ContactID = " +
 82InBlock.gif        "       E.ContactID ON SOH.SalesPersonID = " +
 83InBlock.gif        "       E.EmployeeID LEFT OUTER JOIN " +
 84InBlock.gif        "       Purchasing.ShipMethod SM ON SOH.ShipMethodID " +
 85InBlock.gif        "       = SM.ShipMethodID LEFT OUTER JOIN Sales.Store" +
 86InBlock.gif        "        S ON SOH.CustomerID = S.CustomerID " +
 87InBlock.gif        "WHERE  (SOH.SalesOrderNumber = @SalesOrderNumber)";
 88InBlock.gif
 89InBlock.gif    SqlConnection connection = new
 90InBlock.gif        SqlConnection("Data Source=(local); " +
 91InBlock.gif                      "Initial Catalog=AdventureWorks; " +
 92InBlock.gif                      "Integrated Security=SSPI");
 93InBlock.gif
 94InBlock.gif    SqlCommand command =
 95InBlock.gif        new SqlCommand(sqlSalesOrder, connection);
 96InBlock.gif
 97InBlock.gif    command.Parameters.Add(
 98InBlock.gif        new SqlParameter("SalesOrderNumber",
 99InBlock.gif        salesOrderNumber));
100InBlock.gif
101InBlock.gif    SqlDataAdapter salesOrderAdapter = new
102InBlock.gif        SqlDataAdapter(command);
103InBlock.gif
104InBlock.gif    salesOrderAdapter.Fill(dsSalesOrder, "SalesOrder");
105ExpandedBlockEnd.gif}

106 None.gif
107 None.gif private   void  GetSalesOrderDetailData( string  salesOrderNumber,
108 None.gif                        ref  DataSet dsSalesOrder)
109 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
110InBlock.gif    string sqlSalesOrderDetail =
111InBlock.gif        "SELECT  SOD.SalesOrderDetailID, SOD.OrderQty, " +
112InBlock.gif        "        SOD.UnitPrice, CASE WHEN " +
113InBlock.gif        "        SOD.UnitPriceDiscount IS NULL THEN 0 " +
114InBlock.gif        "        ELSE SOD.UnitPriceDiscount END AS " +
115InBlock.gif        "        UnitPriceDiscount, SOD.LineTotal, " +
116InBlock.gif        "        SOD.CarrierTrackingNumber, " +
117InBlock.gif        "        SOD.SalesOrderID, P.Name, P.ProductNumber " +
118InBlock.gif        "FROM    Sales.SalesOrderDetail SOD INNER JOIN " +
119InBlock.gif        "        Production.Product P ON SOD.ProductID = " +
120InBlock.gif        "        P.ProductID INNER JOIN " +
121InBlock.gif        "        Sales.SalesOrderHeader SOH ON " +
122InBlock.gif        "        SOD.SalesOrderID = SOH.SalesOrderID " +
123InBlock.gif        "WHERE   (SOH.SalesOrderNumber = @SalesOrderNumber) " +
124InBlock.gif        "ORDER BY SOD.SalesOrderDetailID";
125InBlock.gif
126InBlock.gif    using (SqlConnection connection = new
127InBlock.gif        SqlConnection("Data Source=(local); " +
128InBlock.gif                      "Initial Catalog=AdventureWorks; " +
129InBlock.gif                      "Integrated Security=SSPI"))
130ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
131InBlock.gif
132InBlock.gif        SqlCommand command =
133InBlock.gif            new SqlCommand(sqlSalesOrderDetail, connection);
134InBlock.gif
135InBlock.gif        command.Parameters.Add(
136InBlock.gif            new SqlParameter("SalesOrderNumber",
137InBlock.gif            salesOrderNumber));
138InBlock.gif
139InBlock.gif        SqlDataAdapter salesOrderDetailAdapter = new
140InBlock.gif            SqlDataAdapter(command);
141InBlock.gif
142InBlock.gif        salesOrderDetailAdapter.Fill(dsSalesOrder,
143InBlock.gif            "SalesOrderDetail");
144ExpandedSubBlockEnd.gif    }

145ExpandedBlockEnd.gif}

 VB

None.gif Imports  System.Data
None.gif
Imports  System.Data.SqlClient
None.gif
Imports  Microsoft.Reporting.WebForms
None.gif
ExpandedBlockStart.gifContractedBlock.gifPartial 
Class _Default Class _Default
InBlock.gif    
Inherits System.Web.UI.Page
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Protected Sub Page_Init()Sub Page_Init(ByVal sender As Object, _
InBlock.gif                
ByVal e As System.EventArgs) Handles Me.Init
InBlock.gif
InBlock.gif        
If Not Page.IsPostBack Then
InBlock.gif
InBlock.gif            
'Set the processing mode for the ReportViewer to Local
InBlock.gif
            reportViewer.ProcessingMode = ProcessingMode.Local
InBlock.gif
InBlock.gif            
Dim localReport As LocalReport
InBlock.gif            localReport 
= reportViewer.LocalReport
InBlock.gif
InBlock.gif            localReport.ReportPath 
= "Sales Order Detail.rdlc"
InBlock.gif
InBlock.gif            
Dim dataset As New DataSet("Sales Order Detail")
InBlock.gif
InBlock.gif            
Dim salesOrderNumber As String = "SO43661"
InBlock.gif
InBlock.gif            
'Get the sales order data
InBlock.gif
            GetSalesOrderData(salesOrderNumber, dataset)
InBlock.gif
InBlock.gif            
'Create a report data source for the sales order data
InBlock.gif
            Dim dsSalesOrder As New ReportDataSource()
InBlock.gif            dsSalesOrder.Name 
= "SalesOrder"
InBlock.gif            dsSalesOrder.Value 
= dataset.Tables("SalesOrder")
InBlock.gif
InBlock.gif            localReport.DataSources.Add(dsSalesOrder)
InBlock.gif
InBlock.gif            
'Get the sales order detail data
InBlock.gif
            GetSalesOrderDetailData(salesOrderNumber, dataset)
InBlock.gif
InBlock.gif            
'Create a report data source for the sales 
InBlock.gif
            'order detail data
InBlock.gif
            Dim dsSalesOrderDetail As New ReportDataSource()
InBlock.gif            dsSalesOrderDetail.Name 
= "SalesOrderDetail"
InBlock.gif            dsSalesOrderDetail.Value 
= _
InBlock.gif                dataset.Tables(
"SalesOrderDetail")
InBlock.gif
InBlock.gif            localReport.DataSources.Add(dsSalesOrderDetail)
InBlock.gif
InBlock.gif            
'Create a report parameter for the sales order number 
InBlock.gif
            Dim rpSalesOrderNumber As New ReportParameter()
InBlock.gif            rpSalesOrderNumber.Name 
= "SalesOrderNumber"
InBlock.gif            rpSalesOrderNumber.Values.Add(
"SO43661")
InBlock.gif
InBlock.gif            
'Set the report parameters for the report
InBlock.gif
            Dim parameters() As ReportParameter = {rpSalesOrderNumber}
InBlock.gif            localReport.SetParameters(parameters)
InBlock.gif
InBlock.gif        
End If
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub GetSalesOrderData()Sub GetSalesOrderData(ByVal salesOrderNumber As String, _
InBlock.gif                               
ByRef dsSalesOrder As DataSet)
InBlock.gif
InBlock.gif        
Dim sqlSalesOrder As String = _
InBlock.gif            
"SELECT SOH.SalesOrderNumber, S.Name AS Store, " & _
InBlock.gif            
"       SOH.OrderDate, C.FirstName AS SalesFirstName, " & _
InBlock.gif            
"       C.LastName AS SalesLastName, E.Title AS " & _
InBlock.gif            
"       SalesTitle, SOH.PurchaseOrderNumber, " & _
InBlock.gif            
"       SM.Name AS ShipMethod, BA.AddressLine1 " & _
InBlock.gif            
"       AS BillAddress1, BA.AddressLine2 AS " & _
InBlock.gif            
"       BillAddress2, BA.City AS BillCity, " & _
InBlock.gif            
"       BA.PostalCode AS BillPostalCode, BSP.Name " & _
InBlock.gif            
"       AS BillStateProvince, BCR.Name AS " & _
InBlock.gif            
"       BillCountryRegion, SA.AddressLine1 AS " & _
InBlock.gif            
"       ShipAddress1, SA.AddressLine2 AS " & _
InBlock.gif            
"       ShipAddress2, SA.City AS ShipCity, " & _
InBlock.gif            
"       SA.PostalCode AS ShipPostalCode, SSP.Name " & _
InBlock.gif            
"       AS ShipStateProvince, SCR.Name AS " & _
InBlock.gif            
"       ShipCountryRegion, CC.Phone AS CustPhone, " & _
InBlock.gif            
"       CC.FirstName AS CustFirstName, CC.LastName " & _
InBlock.gif            
"       AS CustLastName " & _
InBlock.gif            
"FROM   Person.Address SA INNER JOIN " & _
InBlock.gif            
"       Person.StateProvince SSP ON " & _
InBlock.gif            
"       SA.StateProvinceID = SSP.StateProvinceID " & _
InBlock.gif            
"       INNER JOIN Person.CountryRegion SCR ON " & _
InBlock.gif            
"       SSP.CountryRegionCode = SCR.CountryRegionCode " & _
InBlock.gif            
"       RIGHT OUTER JOIN Sales.SalesOrderHeader SOH " & _
InBlock.gif            
"       LEFT OUTER JOIN  Person.Contact CC ON " & _
InBlock.gif            
"       SOH.ContactID = CC.ContactID LEFT OUTER JOIN" & _
InBlock.gif            
"       Person.Address BA INNER JOIN " & _
InBlock.gif            
"       Person.StateProvince BSP ON " & _
InBlock.gif            
"       BA.StateProvinceID = BSP.StateProvinceID " & _
InBlock.gif            
"       INNER JOIN Person.CountryRegion BCR ON " & _
InBlock.gif            
"       BSP.CountryRegionCode = " & _
InBlock.gif            
"       BCR.CountryRegionCode ON SOH.BillToAddressID " & _
InBlock.gif            
"       = BA.AddressID ON  SA.AddressID = " & _
InBlock.gif            
"       SOH.ShipToAddressID LEFT OUTER JOIN " & _
InBlock.gif            
"       Person.Contact C RIGHT OUTER JOIN " & _
InBlock.gif            
"       HumanResources.Employee E ON C.ContactID = " & _
InBlock.gif            
"       E.ContactID ON SOH.SalesPersonID = " & _
InBlock.gif            
"       E.EmployeeID LEFT OUTER JOIN " & _
InBlock.gif            
"       Purchasing.ShipMethod SM ON SOH.ShipMethodID " & _
InBlock.gif            
"       = SM.ShipMethodID LEFT OUTER JOIN Sales.Store" & _
InBlock.gif            
"        S ON SOH.CustomerID = S.CustomerID " & _
InBlock.gif            
"WHERE  (SOH.SalesOrderNumber = @SalesOrderNumber)"
InBlock.gif
InBlock.gif        Using connection 
As New SqlConnection( _
InBlock.gif                      
"Data Source=(local); " & _
InBlock.gif                      
"Initial Catalog=AdventureWorks; " & _
InBlock.gif                      
"Integrated Security=SSPI")
InBlock.gif
InBlock.gif            
Dim command As New SqlCommand(sqlSalesOrder, connection)
InBlock.gif
InBlock.gif            
Dim parameter As New SqlParameter("SalesOrderNumber", _
InBlock.gif                salesOrderNumber)
InBlock.gif            
command.Parameters.Add(parameter)
InBlock.gif
InBlock.gif            
Dim salesOrderAdapter As New SqlDataAdapter(command)
InBlock.gif
InBlock.gif            salesOrderAdapter.Fill(dsSalesOrder, 
"SalesOrder")
InBlock.gif
InBlock.gif        
End Using
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub GetSalesOrderDetailData()Sub GetSalesOrderDetailData( _
InBlock.gif                           
ByVal salesOrderNumber As String, _
InBlock.gif                           
ByRef dsSalesOrder As DataSet)
InBlock.gif
InBlock.gif        
Dim sqlSalesOrderDetail As String = _
InBlock.gif            
"SELECT  SOD.SalesOrderDetailID, SOD.OrderQty, " & _
InBlock.gif            
"        SOD.UnitPrice, CASE WHEN " & _
InBlock.gif            
"        SOD.UnitPriceDiscount IS NULL THEN 0 " & _
InBlock.gif            
"        ELSE SOD.UnitPriceDiscount END AS " & _
InBlock.gif            
"        UnitPriceDiscount, SOD.LineTotal, " & _
InBlock.gif            
"        SOD.CarrierTrackingNumber, " & _
InBlock.gif            
"        SOD.SalesOrderID, P.Name, P.ProductNumber " & _
InBlock.gif            
"FROM    Sales.SalesOrderDetail SOD INNER JOIN " & _
InBlock.gif            
"        Production.Product P ON SOD.ProductID = " & _
InBlock.gif            
"        P.ProductID INNER JOIN " & _
InBlock.gif            
"        Sales.SalesOrderHeader SOH ON " & _
InBlock.gif            
"        SOD.SalesOrderID = SOH.SalesOrderID " & _
InBlock.gif            
"WHERE   (SOH.SalesOrderNumber = @SalesOrderNumber) " & _
InBlock.gif            
"ORDER BY SOD.SalesOrderDetailID"
InBlock.gif
InBlock.gif        Using connection 
As New SqlConnection( _
InBlock.gif                      
"Data Source=(local); " & _
InBlock.gif                      
"Initial Catalog=AdventureWorks; " & _
InBlock.gif                      
"Integrated Security=SSPI")
InBlock.gif
InBlock.gif            
Dim command As New SqlCommand(sqlSalesOrderDetail, _
InBlock.gif                                          connection)
InBlock.gif
InBlock.gif            
Dim parameter As New SqlParameter("SalesOrderNumber", _
InBlock.gif                salesOrderNumber)
InBlock.gif            
command.Parameters.Add(parameter)
InBlock.gif
InBlock.gif            
Dim salesOrderDetailAdapter As New SqlDataAdapter(command)
InBlock.gif
InBlock.gif            salesOrderDetailAdapter.Fill(dsSalesOrder, _
InBlock.gif                
"SalesOrderDetail")
InBlock.gif
InBlock.gif        
End Using
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedBlockEnd.gif
End Class

 

三、设计报表连接数据源

        当做完以上工作后,还需要设计报表的连接数据源,用记事本打开Sales Order Detail.rdlc。你只需详细关注里面的DataSources和DataSets就可

 1 None.gif   < DataSources >
 2 None.gif
 3 None.gif     < DataSource  Name ="AdventureWorks" >        --报表数据源名称必须统一
 4 None.gif
 5 None.gif         < ConnectionProperties >
 6 None.gif
 7 None.gif         < ConnectString  />
 8 None.gif
 9 None.gif         < DataProvider > SQL </ DataProvider >
10 None.gif
11 None.gif       </ ConnectionProperties >
12 None.gif
13 None.gif     </ DataSource >
14 None.gif
15 None.gif  </ DataSources >
16 None.gif
17 None.gif  < DataSets >
18 None.gif
19 None.gif    < DataSet  Name ="SalesOrderDetail" >           --对应于程序中dsSalesOrderDetail.Name = "SalesOrderDetail"
20 None.gif
21 None.gif       < Fields >                                                       --所有dataset中有的列必须在此申明
22 None.gif
23 None.gif         < Field  Name ="SalesOrderDetailID" >
24 None.gif
25 None.gif           < DataField > SalesOrderDetailID </ DataField >
26 None.gif
27 None.gif           < rd:TypeName > System.Int32 </ rd:TypeName >
28 None.gif
29 None.gif         </ Field >
30 None.gif
31 None.gif       </ Fields >
32 None.gif
33 None.gif       < Query >
34 None.gif
35 None.gif         < DataSourceName > AdventureWorks </ DataSourceName >
36 None.gif
37 None.gif         < CommandText >
38 None.gif
39 None.gif        </ CommandText >
40 None.gif
41 None.gif         < Timeout > 30 </ Timeout >
42 None.gif
43 None.gif         < rd:UseGenericDesigner > true </ rd:UseGenericDesigner >
44 None.gif
45 None.gif       </ Query >
46 None.gif
47 None.gif     </ DataSet >
48 None.gif
49 None.gif  </ DataSets >
50 None.gif


        其中DataSet 的<DataSet Name="SalesOrderDetail">属性对应于程序中的

 Dim dsSalesOrderDetail As New ReportDataSource()

 dsSalesOrderDetail.Name = "SalesOrderDetail"//对应该属性

 dsSalesOrderDetail.Value =dataset.Tables("SalesOrderDetail")//绑定已经存在的dataset

 localReport.DataSources.Add(dsSalesOrderDetail)

        只要rdlc文件符合规范,其中的Fields设置为所有得出列名,定义的datasource name没有规定,但是必须统一,既可。你只要按照这样的格式去写一个报表文件,那么任何dataset都是可以绑定上去的,也就是说比如从将一个存储过程返回的dataset绑定到一张报表上面。

提示:

        所有的列名申明将是一个非常令人头疼的问题,我所能作到的最简单的方法是按照向导作出一张报表,然后提取里面的所有fields复制到我要产生的报表中,如果你有更好的方法还请尽快告诉我,谢谢。

    最后就是在报表上增加你想要显示的数据了,增加Reports Item,绑定参数等等,在此不作为本文介绍的内容。

四、总结

        对于动态绑定报表的优点实在是太多了,你可以像绑定一个DataGrid那样去绑定一张报表,而且Reporting Services支持强大导出的功能也会让你的报表更满足你的需要。你可以从不同的页面传递不同的参数到同一个报表显示页面,根据参数的不同显示不同的报表,后台的代码是非常简单,唯一比较繁琐的任务都在于报表本身的设计。

       呵呵,总算写完了,如果你有更好的方法还请一定联系我,大家交流,共同进步。
    
        示例程序代码:/Files/Carlwave/reportSample.rar(要求:安装了SQL2005和VS2005即可)

转载于:https://www.cnblogs.com/ghx88/archive/2006/12/25/603102.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值