一、综述

        这里我要介绍的是如何使用
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#

 1VS2005+SQL2005 Reporting Service动态绑定报表(Web)protected void Page_Init(object sender, EventArgs e)
 2}

 VB

 1VS2005+SQL2005 Reporting Service动态绑定报表(Web)Imports Microsoft.Reporting.WebForms
 2VS2005+SQL2005 Reporting Service动态绑定报表(Web)
 3End 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.rdlSales Order Detail.rdlc

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

 C#

  1VS2005+SQL2005 Reporting Service动态绑定报表(Web)protected void Page_Init(object sender, EventArgs e)
  2}

 VB

VS2005+SQL2005 Reporting Service动态绑定报表(Web)Imports System.Data
VS2005+SQL2005 Reporting Service动态绑定报表(Web)
Imports System.Data.SqlClient
VS2005+SQL2005 Reporting Service动态绑定报表(Web)
Imports Microsoft.Reporting.WebForms
VS2005+SQL2005 Reporting Service动态绑定报表(Web)
End Class

 

三、设计报表连接数据源

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

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


        其中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即可)

相关文章: