如何在SSRS中传递参数
问题描述:
我对SSRS很新颖,并且一直在传递参数。目前我正在做以下几点:如何在SSRS中传递参数
ReportViewer1.ServerReport.ReportPath = ConfigurationManager.AppSettings["serverPath"] + "MyReport";
List<ReportParameter> paralist = new List<ReportParameter>();
ReportParameter reportParam1 = new ReportParameter("LocationId", txtLocationId.Text);
ReportParameter reportParam2 = new ReportParameter("PrdAcctId", txtProductAccountId.Text);
ReportParameter reportParam3 = new ReportParameter("FromDate", frmdt);
ReportParameter reportParam4 = new ReportParameter("ToDate", todt);
ReportParameter reportParam5 = new ReportParameter("IntAmt", todt);
paralist.Add(reportParam1);
paralist.Add(reportParam2);
paralist.Add(reportParam3);
paralist.Add(reportParam4);
paralist.Add(reportParam5);
ReportViewer1.ServerReport.SetParameters(paralist);
这个工作正常,当没有。的参数设置。但在某些情况下,没有。的参数将在运行时根据用户输入来决定。
现在,我在如何使没有损失。在.rdl文件中灵活的参数。例如,根据用户输入可以有n个FromDate和ToDate以及相应的IntAmt。
我很感激任何帮助,并提前致谢。
答
可以调用GetParameters方法通过他们把所有的当前报告参数和循环来看看各是各的各种属性:
开始:
ReportParameterInfoCollection param = ReportViewer1.ServerReport.GetParameters();
答
你可以尝试以下操作,它的工作原理 - 用您的参数的序号值替换索引0:
if (this.rptView.ServerReport.GetParameters()[0].Values.Count > 0)
{
string parvalue = this.rptView.ServerReport.GetParameters()[0].Values.ToString();
paramList.Add(new ReportParameter("ReportName", parvalue));
}
答
这不是直接回答你的问题。我们的主页面使用window.href
重定向到报告呈现页面,并将参数作为查询字符串传递。
注:这是VB.Net代码,你可以转换成等价的C#代码
后面的代码
Public Function GetParametersList(ByVal reportRDLName As String) As List(Of String)
'Get these values from database. Following is a sample
Dim parametersList As New List(Of String)()
parametersList.Add("plantcd")
parametersList.Add("CountID")
Return parametersList
End Function
Public Function GetReportPath(ByVal reportRDLName As String) As String
'Get these values from database. Following is a sample
Dim reportPath As String = "/MyFolder/MyModule/"
Return reportPath
End Function
Protected Overrides Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
'--------------------------------------------------------
Me.EnableViewState = True
If Not Page.IsPostBack Then
vwreports.ServerReport.ReportServerCredentials = New MyCustomReportServerCredentials
vwreports.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote
vwreports.ServerReport.ReportServerUrl = New Uri(ConfigurationManager.AppSettings("MyReportServer"))
Dim reportRDLName As String = Request.QueryString("rname").ToString()
Dim parametersList As List(Of String) = GetParametersList(reportRDLName)
Dim reportPath As String = GetReportPath(reportRDLName)
Dim countOfParameters As Integer = parametersList.Count
Dim parms As ReportParameter() = New ReportParameter(countOfParameters - 1) {}
Dim iterationCount As Integer = 0
For Each parameterName As String In parametersList
Dim parameterValue As String = Convert.ToString(Request.QueryString(parameterName))
If Not [String].IsNullOrEmpty(parameterValue) Then
parms(iterationCount) = New ReportParameter(parameterName, parameterValue)
iterationCount += 1
End If
Next
vwreports.ServerReport.ReportPath = reportPath + reportRDLName
vwreports.ServerReport.SetParameters(parms)
vwreports.ShowParameterPrompts = False
vwreports.ServerReport.Refresh()
End If
End Sub
检查这一个:http://stackoverflow.com/questions/3215350/ rdl-is-it-it-possible-have-optional-parameter-or-force-default-value-to-null –
以及另一个:http://bloggingabout.net/blogs/egiardina/archive/2007/06 /26/sql-server-reporting-services-optional-parameters.aspx –
感谢Davide的努力,但在我的情况下没有。的参数没有设置。传递null不是问题。用户可以只输入一个FromDate,ToDate和IntAmt,或者他可以输入几个。我想要的是访问所有这些是SSRS。我认为串联这些值然后将它们分解到SSRS可能会工作 – Arcturus