如何以编程方式将参数传递给SSRS报告

时间:2008-12-15 15:47:32

标签: asp.net vb.net reporting-services

我正在寻找一些帮助,通过VB.NET和ASP.NET以编程方式将参数传递给SSRS报告。这似乎应该是一个相对简单的事情,但我没有太多运气找到帮助。

有没有人建议去哪里寻求帮助,或者甚至是一些示例代码?

感谢。

4 个答案:

答案 0 :(得分: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 :(得分:11)

如果可直接访问报表服务器,则可以在使用URL访问repoort时在Querystring中传递参数:

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

您可以通过在网址末尾添加以下内容来添加输出格式:

&安培; RS:格式= Excel中

&安培; RS:格式= PDF

答案 2 :(得分: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();

答案 3 :(得分:1)

我做了这段代码已经有一段时间了,但它可能有所帮助: 您的Web项目必须是一个Web站点,而不是“ASP.Net Web Application”类型的项目,否则您将无法添加下面提到的参考。 右键单击该项目并添加一个ASP.Net文件夹 - App_WebReferences。您必须指定SRS所在的服务器;选择.asmx。 一旦添加,该级别下的文件夹称为RSService,其下有两件事:reportservice.discomap&amp;的.wsdl。 在我的VB中,我做了Imports RSService和Imports System.Web.Services.Protocols,然后......

Dim MyRS As New ReportingService

报告服务与应用程序所在的网络服务器位于不同的服务器上,因此我无法执行以下操作:MyRS.Credentials = System.Net.CredentialCache.DefaultCredentials

相反:MyRS.Credentials = New System.Net.NetworkCredential(rs1, rs2, rs3), 其中rs1 / 2/3是登录SRS框,密码是SRS框,&amp;域名“。(这些在我的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