折腾了我一下午,终于是搞定了,记录下来。
若需要在VS2010中为.net 2.0的网站添加RDLC报表,需要手工编辑RDLC文件指定对应的数据集。因为VS2010采用了新的数据源设计向导,因此在旧的环境下无法应用设计向导添加配置相应的数据源、数据集,以及他们之间的关联关系。
具体步骤:
1.在App_Code下添加一个数据集,我这里起名为:LR.xsd,双击打开进行编辑,如下图:
右击进行配置,sql语句填写select from即可
_
2.在网站根目录下的Report文件夹下添加一个网页:TestReport.aspx
_
3.在TestReport.aspx中添加一个ReportViewer用来显示报表
_
4.由于报表设计器已经与.net 2.0不同,所以最好在另一个纯.net 4.0项目中将RDLC报表设计好,然后把设计好的报表文件加入项目即可
_
5.在TestReport.aspx中拖入一个ObjectDataSource,数据源配置选择:LRTableAdapter
_
6.此时,右键RDLC报表的数据行,选择Tablix属性,会发现rdlc报表Tablib属性下的数据集名称选择列是空的,可我们明明建立好了数据集LR的啊。原因就在于RDLC文件中
_
7.RDLC是一个XML格式的文件,由于我们不是通过设计器设计的,因此我们还需要配置RDLC的数据集
_
8.F5执行一下TestReport,编译不过,会自动打开RDLC报表的XML源文件,应该如下图:
_
9.在<BODY>标签之前,<Report>标签之后,加上如下代码,当然,具体名称还是要按照情况做修改,改动很小
< DataSources >
< DataSource Name ="LR" >
< ConnectionProperties >
< DataProvider > System.Data.DataSet </ DataProvider >
< ConnectString > /* Local Connection */ </ ConnectString >
</ ConnectionProperties >
< rd:DataSourceID > 095487b0-b3d0-4cd9-84e4-142ee402b86c </ rd:DataSourceID >
</ DataSource >
</ DataSources >
< DataSets >
< DataSet Name ="LR" >
< Fields >
< Field Name ="Description" >
< DataField > Description </ DataField >
< rd:TypeName > System.String </ rd:TypeName >
</ Field >
< Field Name ="Value" >
< DataField > Value </ DataField >
< rd:TypeName > System.String </ rd:TypeName >
</ Field >
< Field Name ="EventStampUTC" >
< DataField > EventStampUTC </ DataField >
< rd:TypeName > System.DateTime </ rd:TypeName >
</ Field >
</ Fields >
< Query >
< DataSourceName > LR </ DataSourceName >
< CommandText > /* Local Query */ </ CommandText >
</ Query >
< rd:DataSetInfo >
< rd:DataSetName > LR </ rd:DataSetName >
< rd:SchemaPath > D:\temp\Vss_FUSYS_Newest\WebUI\App_Code\LR.xsd </ rd:SchemaPath >
< rd:TableName > TestReport </ rd:TableName >
< rd:TableAdapterFillMethod > Fill </ rd:TableAdapterFillMethod >
< rd:TableAdapterGetDataMethod > GetData </ rd:TableAdapterGetDataMethod >
< rd:TableAdapterName > LRTableAdapter </ rd:TableAdapterName >
</ rd:DataSetInfo >
</ DataSet >
</ DataSets >
_
10.Web.Config文件中,加入数据库连接字符串:
< connectionStrings >
< add name ="WWALMDBConnectionString" connectionString ="Data Source=.;Initial Catalog=WWAlarmDB_ZZ_20100726;User ID=sa;Password=sqlsql"
providerName ="System.Data.SqlClient" />
_
11.在TestReport.aspx.cs后台文件中,在需要的地方加入如下查询代码
// 连接字符串
string connstr = " Data Source=.;Initial Catalog=WWAlarmDB_ZZ_20100726;User ID=sa;Password=sqlsql " ;
// 新建连接
SqlConnection connection = new SqlConnection(connstr);
// 查询命令
SqlCommand command = new SqlCommand(TextBoxSQL.Text, connection);
// 适配器
SqlDataAdapter adapter = new SqlDataAdapter(command);
// 数据集
DataSet ds = new DataSet();
// 查询并填充
try
{
connection.Open();
adapter.Fill(ds);
}
catch (Exception ex)
{
}
finally
{
connection.Close();
command.Dispose();
connection.Dispose();
}
// 指定rdlc报表
// this.ReportViewer1.LocalReport.ReportEmbeddedResource = "TestReport.rdlc";
// 清空reportviewer先前的数据源
this .ReportViewer1.LocalReport.DataSources.Clear();
// 新建数据源
ReportDataSource rs = new ReportDataSource();
rs.Name = " LR " ; // "EventReport"是rdlc报表Tablib属性下的数据集名称
rs.Value = ds.Tables[ 0 ];
// 添加数据源
this .ReportViewer1.LocalReport.DataSources.Add(rs);
// 设置页面宽度类型
this .ReportViewer1.ZoomMode = ZoomMode.PageWidth;
_
至此,大功告成。当然,实际中可能还有一些问题,但都不是关键的了,根据报错的情况是很好排除的。