2012-04-09 124 views
1

我有一些使用SQL Server BI Development Studio创建的RDL报表,现在我需要使用ASP.NET Report Viewer呈现它们。即使我的RDL包含对SQL服务器和SELECT查询的引用,它仍会说我需要为报告指定一个数据源。有没有办法使RDL中的数据源被使用,或者我是否必须通过C#代码将数据源传递给报表查看器?带ReportViewer的RDL报表的数据源

谢谢。

回答

1

您是否验证过RDL中的DataSourceReference元素?它需要报告服务器的路径。

的DataSourceReference元件可以包含一个完整的文件夹路径( 例如,/ SampleReports/AdventureWorks的)或相对路径( 例如,AdventureWorks的)。相对路径从报告中的 开始。共享数据源必须与 报告位于同一台服务器上。

也验证DataSourceID。看看answer on this question。它看起来可能是你遇到的同样的问题。

如果您使用的是RDLC,还可以使用ReportDataSource手动设置报告的数据源。下例中的“GetMyData”将实现IEnumerable或IDataSource。

ReportDataSource reportDataSource = new ReportDataSource("MyDataName", GetMyData(startAt, endAt)); 
ReportViewer1.LocalReport.DataSources.Add(reportDataSource); 
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reporting/MyReport.rdlc"); 

ReportParameterCollection col = new ReportParameterCollection(); 
ReportParameter startAtParam = new ReportParameter("StartAt", startAt.ToString("MMM, dd yyyy")); 
col.Add(startAtParam); 
ReportParameter endAtParam = new ReportParameter("EndAt", endAt.ToString("MMM, dd yyyy")); 
col.Add(endAtParam); 

ReportViewer1.LocalReport.SetParameters(col); 

如果你是一个RDL转换成RDLC可以follow the steps here。请注意,您需要重新创建数据源和查询信息。此外,XML模式定义是不同的2005年至2008年间

+0

我想我在搅拌t在这里上演。我研究了一些,发现我真正需要的是RDLC,因为我想使用报告查看器呈现报告定义而不涉及报告服​​务器。不过,我需要使用RDL(C)文件中的数据集和数据源。我正确的方向吗? – 2012-04-09 13:43:13

+0

但是,仅仅将RDL转换为RDLC,保留来自RDL的数据集和数据源规范并使用它们而无需执行所有声明,是不可能的?我的意思是,如果我已经有一个查询和RDL上指定的SQL Server的路径,我不想再做所有事情,因为我不再在服务器上呈现该报告。希望我清楚。 – 2012-04-09 14:28:23

+0

您制作报告的Visual Studio版本是什么?查看“如何转换报表定义”步骤[此处](http://msdn.microsoft.com/zh-cn/library/ms252109(v = vs.90).aspx) – 2012-04-09 14:55:47

5

我想你是在本地模式下使用报表查看器:

viewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local 

并使用viewer.LocalReport。在这种情况下,您必须自己执行查询并将结果传递给查看者:

dim tbl as new DataTable() 

填充数据例如tbl.load(DataReader的)。

Dim VDS As New ReportDataSource 
VDS.Name = "Your Data Source Name" 
VDS.Value = tbl 
viewer.LocalReport.DataSources.Add(VDS) 

如果你想使用服务器模式

viewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote 

您必须使用viewer.ServerReport

viewer.ServerReport.ReportServerUrl = New Uri(ReportServerURL) 

,可能viewer.ServerReport.SetDataSourceCredentials

Reporting Services and ReportViewer Controls in Visual Studio

编辑
如何从RDL查询
初始化:

Dim XRep As New XmlDocument 
XRep.Load(ReportPath) 
Dim xmlnsManager As New System.Xml.XmlNamespaceManager(XRep.NameTable) 
dim DefaultNSURI as string = XRep.GetElementsByTagName("Width")(0).NamespaceURI 
xmlnsManager.AddNamespace("rep", DefaultNSURI) 

数据集处理:

For Each nd As XmlNode In XRep.SelectNodes("/rep:Report/rep:DataSets/rep:DataSet", xmlnsManager) 
    'DataSourceName can be used to find iformation about connection' 
    Dim DataSourceName As String = nd.SelectSingleNode("rep:Query/rep:DataSourceName", xmlnsManager).InnerText 
    Dim DSName As String = nd.Attributes("Name").Value  
    cmd.CommandText = nd.SelectSingleNode("rep:Query/rep:CommandText", xmlnsManager).InnerText 
    Dim tbl As New DataTable(DSName) 
    tbl.Load(cmd.ExecuteReader) 
    'The table created here is to be passed to LocalReport as datasource' 
Next 

注 要转换vb.net < - > C#我用Convert VB.NET to C#

相关问题