2012-04-09 126 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#

相關問題