2008-12-15 178 views
14

我正在尋找一些幫助,以通過VB.NET和ASP.NET以編程方式將參數傳遞給SSRS報告。這似乎應該是一個相對簡單的事情,但我沒有多少運氣找到這方面的幫助。如何以編程方式將參數傳遞給SSRS報告

有沒有人有什麼建議去哪裏得到這個幫助,或者甚至一些示例代碼?

謝謝。

回答

15

您可以執行以下操作::(它可以在本地報告中使用,例如在Full Blown SSRS報告中。但在全模式下,使用適當的類,參數部分保持不變)

LocalReport myReport = new LocalReport(); 
myReport.ReportPath = Server.MapPath("~/Path/To/Report.rdlc"); 

ReportParameter myParam = new ReportParameter("ParamName", "ParamValue"); 
myReport.SetParameters(new ReportParameter[] { myParam }); 

// more code here to render report 
+1

如果我不使用本地報告但不希望在url上留下顯式值,該怎麼辦? – Leonardo 2015-02-24 14:20:46

11

如果報表服務器直接訪問,則可以在查詢字符串傳遞參數,如果你是一個URL訪問repoort:

http://MyServer/ReportServer/?MyReport&rs:Command=Render&Param1=54321&Param2=product

您可以通過添加在年底下面添加輸出格式

& RS:URL的格式= Excel中

& rs:格式= PDF

+1

將這些參數作爲URL傳遞似乎是爲別人破解報告打開大門。對於一個內部網站來說這可能沒問題,但可能不適合面向公衆的網站。 – LunaCrescens 2009-07-06 20:59:12

1

這已經有一段時間,因爲我沒有這個代碼,但它可以幫助: 您的Web項目必須有一個網站,而不是「ASP.Net Web Application」類型的項目,否則您將無法添加下面提到的參考。 右鍵單擊項目並添加一個ASP.Net文件夾--App_WebReferences。您必須指定SRS所在的服務器;選擇.asmx。 添加後,該級別下的文件夾被稱爲RSService,在該文件夾下有兩件事:reportservice.discomap & .wsdl。 在我的VB,我做進口RSService和進口System.Web.Services.Protocols,然後...

Dim MyRS As New ReportingService 

報告服務是一個不同的服務器的網絡服務器應用程序是對的,所以我」做T如下:MyRS.Credentials = System.Net.CredentialCache.DefaultCredentials

相反:MyRS.Credentials = New System.Net.NetworkCredential(rs1, rs2, rs3), 其中RS1/2/3是登錄SRS箱,密碼箱SRS,&域名」(這些被加密在我的web.config)

然後,批量粘貼:

MyRS.Credentials = New System.Net.NetworkCredential(rs1, rs2, rs3) 

Dim ReportByteArray As Byte() = Nothing 
Dim ReportPath As String = "/SRSSiteSubFolder/ReportNameWithoutRDLExtension" 
Dim ReportFormat As String = "PDF" 
Dim HistoryID As String = Nothing 
Dim DevInfo As String = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>" 
'Dim x As ReportParameter - not necessary 
Dim ReportParams(0) As ParameterValue 
ReportParams(0) = New ParameterValue() 
ReportParams(0).Name = "TheParamName" 
ReportParams(0).Value = WhateverValue 

Dim Credentials As DataSourceCredentials() = Nothing 
Dim ShowHideToggle As String = Nothing 
Dim Encoding As String 
Dim MimeType As String 
Dim ReportHistoryParameters As ParameterValue() = Nothing 
Dim Warnings As Warning() = Nothing 
Dim StreamIDs As String() = Nothing 
'Dim sh As New SessionHeader() - not necessary 
''MyRS.SessionHeaderValue = sh - not necessary 

ReportByteArray = MyRS.Render(ReportPath, ReportFormat, HistoryID, DevInfo, ReportParams, Credentials, _ 
    ShowHideToggle, Encoding, MimeType, ReportHistoryParameters, Warnings, StreamIDs) 
'(Yay! That line was giving "HTTP error 401 - Unauthorized", until I set the credentials 
' as above, as explained by http://www.odetocode.com/Articles/216.aspx.) 

'Write the contents of the report to a PDF file: 
Dim fs As FileStream = File.Create(FullReportPath, ReportByteArray.Length) 
fs.Write(ReportByteArray, 0, ReportByteArray.Length) 
fs.Close() 

Call EmailTheReport(FullReportPath) 

If IO.File.Exists(FullReportPath) Then 
    IO.File.Delete(FullReportPath) 
End If 
2
ReportViewer1.LocalReport.DataSources.Clear(); 
ReportViewer1.Reset(); 
Label1.Visible = false; 
ReportViewer1.Visible = true; 
DataSet dataSet = new DataSet(); 
dataSet = new ClassBLL().Load_Report_Detail(TextBox1.Text, 
ddlType.SelectedValue, levelcode, fields); 
ReportDataSource datasource = new ReportDataSource("DataSet_StoreprocedureName", 
dataSet.Tables[0]); 

if (dataSet.Tables[0].Rows.Count == 0) 
{ 
    ReportViewer1.Visible = false; 
} 

ReportViewer1.LocalReport.ReportPath = Server.MapPath("") + @"\Report.rdlc"; 
ReportViewer1.LocalReport.DataSources.Clear(); 
ReportViewer1.LocalReport.DataSources.Add(datasource); 
string fields="name,girish,Z0117"; 
string[] filedName = fields.Split(','); 
ReportParameter[] param = new ReportParameter[2]; 

//for (int i = 0; i < filedName.Length; i++) 
//{ 

param[0] = new ReportParameter(filedName[0], filedName[0], true); 
param[1] = new ReportParameter(filedName[3], filedName[3], true); 

// } 


ReportViewer1.LocalReport.SetParameters(param); 

ReportViewer1.ServerReport.Refresh(); 
+1

請在您的代碼中添加一些註釋。 – 2012-11-18 10:57:45

相關問題