Reporting Services报告中的自定义代码

时间:2021-08-14 08:08:55

In Reporting Services I would like to add a parameter that contains data from a custom code block. Ideally, I would be able to run the following code (this is a simple testing example):

在Reporting Services中,我想添加一个包含自定义代码块数据的参数。理想情况下,我将能够运行以下代码(这是一个简单的测试示例):

Function GetPeriods() As String()
 Dim values As System.Collections.ArrayList = 
    New System.Collections.ArrayList()
 For i as integer = 1 to 24
    values.Add(i)
 Next
 Return values.ToArray()
End Function

and put the following in the "Text Field" of the parameter:

并将以下内容放在参数的“文本字段”中:

=Code.GetPeriods()

However, when I run the report, the parameter I apply this to is disabled and empty. Is there a different technique that should be used? Or am I doing something wrong?

但是,当我运行报表时,我应用此参数的参数被禁用并为空。是否应该使用不同的技术?或者我做错了什么?

7 个解决方案

#1


2  

If you're using SQL 2008 Reporting Services then you can have a look at this page which introduces the concept of using custom assemblies.

如果您正在使用SQL 2008 Reporting Services,那么您可以查看此页面,其中介绍了使用自定义程序集的概念。

If you're using SQL 2005 Reporting Services then this link is the one you want.

如果您使用的是SQL 2005 Reporting Services,那么此链接就是您想要的链接。

It's a mostly trivial thing, simply compile your code into a class library and follow the instructions provided to allow your report to reference it.

这是一件非常简单的事情,只需将您的代码编译成类库并按照提供的说明进行操作即可让您的报告引用它。

#2


2  

You are returning an array item (an array of strings) into a text field. Instead, try returning a plain string. That should work. If you would still like to return an array list, you must basically bind it to a list control in your RDL. You can definitely do that with dataset extensions. However, I am not sure if there is any other easy way. Check the proprties of the list control and see if it allows you to directly bind to an array list.

您将一个数组项(一个字符串数组)返回到文本字段。相反,尝试返回一个纯字符串。这应该工作。如果您仍想返回数组列表,则必须将其基本绑定到RDL中的列表控件。你绝对可以使用数据集扩展来做到这一点。但是,我不确定是否还有其他简单方法。检查列表控件的属性,看看它是否允许您直接绑定到数组列表。

#3


1  

You can create the same stored procedure on SQL Server and load parameter values from that procedure.

您可以在SQL Server上创建相同的存储过程,并从该过程加载参数值。

#4


1  

To access your members/functions implemented in custom code of SSRS report you should set the access modifier to "Public":

要访问在SSRS报告的自定义代码中实现的成员/功能,您应将访问修饰符设置为“公共”:

Public Function GetPeriods() As String
...

see article Writing Custom Code in SQL Server Reporting Services

请参阅SQL Server Reporting Services中的编写自定义代码一文

#5


1  

I've been trying to do this same thing, set a simple list of parameter values from report code. None of the links in any of these answers shows how to do this and after quite a bit of digging around I don't think it's even possible. Yes it is possible to get the values from a database query, from a web service, or from a custom assembly, but each of these creates a lot of overhead compared to getting the list from a simple function call like =Code.GetValues(), where the function uses a For loop to create the values.

我一直在尝试做同样的事情,从报告代码中设置一个简单的参数值列表。这些答案中的任何一个链接都没有显示如何做到这一点,经过相当多的挖掘,我认为它甚至不可能。是的,可以从数据库查询,Web服务或自定义程序集中获取值,但与从类似= Code.GetValues()的简单函数调用获取列表相比,每个都会产生大量开销。 ,函数使用For循环创建值。

msvcyc is correct in pointing out that the parameter is expecting a string value, but the function is returning an array. I changed the return type to Array as suggested by prashant sable, but the select list is still grayed out, it does not work. And coldice is correct in saying that the access modifier should be Public.

msvcyc指出该参数需要一个字符串值,但该函数返回一个数组是正确的。我按照prashant sable的建议将返回类型更改为Array,但是选择列表仍然是灰色的,它不起作用。并且冷却是正确的,说访问修饰符应该是公共的。

In my digging around I found an article by James Kovac from 2005 that pointed out why this is not possible. The Parameters class has a get method, but no set method. In the VS 2008 object browser for SSRS 2008 the object name has changed, but it still does not contain a set method (see Microsoft.ReportingServices.Interfaces.IParameter.Name or .Value).

在我的挖掘中,我发现James Kovac从2005年开始发表一篇文章,指出为什么这是不可能的。 Parameters类有一个get方法,但没有set方法。在SSRS 2008的VS 2008对象浏览器中,对象名称已更改,但它仍然不包含set方法(请参阅Microsoft.ReportingServices.Interfaces.IParameter.Name或.Value)。

My current workaround is to just hard code the list of values, but if your value list needs to be dynamic then your only choices are database queries, web services, or custom assemblies. I think the easiest workaround of these three is to get the values from the database engine, as suggested by oleksiy.t, as long as you can write a query to return the value list you want. Your list of integers, or my list of time intervals, would both be easy queries to write. Otherwise you will need to use one of the other two workarounds.

我目前的解决方法是硬编码值列表,但如果您的值列表需要是动态的,那么您唯一的选择是数据库查询,Web服务或自定义程序集。我认为这三个最简单的解决方法是从oleksiy.t建议的数据库引擎中获取值,只要您可以编写查询以返回所需的值列表。您的整数列表或我的时间间隔列表都可以轻松查询。否则,您将需要使用另外两种解决方法之一。

#6


1  

I checked your code. The only thing that's wrong is your function returns String(). When I changed your method signature to return Array, it worked fine, in my report.

我检查了你的代码。唯一错误的是你的函数返回String()。当我更改您的方法签名以返回Array时,它在我的报告中工作正常。

Change the signature to Function GetPeriods() As Array

将签名更改为Function GetPeriods()As Array

#7


0  

Everything I've seen requires parameters and their respective settings to be part of the RDL.

我见过的所有内容都需要参数及其各自的设置才能成为RDL的一部分。

That being said, if you're going to "hardcode" the values, you could create a dataset just for the report, perhaps in XML, or if it needs to be programmatically driven, do it in a web service.

话虽这么说,如果您要对这些值进行“硬编码”,您可以为报表创建一个数据集,可能是XML,或者如果需要以编程方式驱动,请在Web服务中执行。

#1


2  

If you're using SQL 2008 Reporting Services then you can have a look at this page which introduces the concept of using custom assemblies.

如果您正在使用SQL 2008 Reporting Services,那么您可以查看此页面,其中介绍了使用自定义程序集的概念。

If you're using SQL 2005 Reporting Services then this link is the one you want.

如果您使用的是SQL 2005 Reporting Services,那么此链接就是您想要的链接。

It's a mostly trivial thing, simply compile your code into a class library and follow the instructions provided to allow your report to reference it.

这是一件非常简单的事情,只需将您的代码编译成类库并按照提供的说明进行操作即可让您的报告引用它。

#2


2  

You are returning an array item (an array of strings) into a text field. Instead, try returning a plain string. That should work. If you would still like to return an array list, you must basically bind it to a list control in your RDL. You can definitely do that with dataset extensions. However, I am not sure if there is any other easy way. Check the proprties of the list control and see if it allows you to directly bind to an array list.

您将一个数组项(一个字符串数组)返回到文本字段。相反,尝试返回一个纯字符串。这应该工作。如果您仍想返回数组列表,则必须将其基本绑定到RDL中的列表控件。你绝对可以使用数据集扩展来做到这一点。但是,我不确定是否还有其他简单方法。检查列表控件的属性,看看它是否允许您直接绑定到数组列表。

#3


1  

You can create the same stored procedure on SQL Server and load parameter values from that procedure.

您可以在SQL Server上创建相同的存储过程,并从该过程加载参数值。

#4


1  

To access your members/functions implemented in custom code of SSRS report you should set the access modifier to "Public":

要访问在SSRS报告的自定义代码中实现的成员/功能,您应将访问修饰符设置为“公共”:

Public Function GetPeriods() As String
...

see article Writing Custom Code in SQL Server Reporting Services

请参阅SQL Server Reporting Services中的编写自定义代码一文

#5


1  

I've been trying to do this same thing, set a simple list of parameter values from report code. None of the links in any of these answers shows how to do this and after quite a bit of digging around I don't think it's even possible. Yes it is possible to get the values from a database query, from a web service, or from a custom assembly, but each of these creates a lot of overhead compared to getting the list from a simple function call like =Code.GetValues(), where the function uses a For loop to create the values.

我一直在尝试做同样的事情,从报告代码中设置一个简单的参数值列表。这些答案中的任何一个链接都没有显示如何做到这一点,经过相当多的挖掘,我认为它甚至不可能。是的,可以从数据库查询,Web服务或自定义程序集中获取值,但与从类似= Code.GetValues()的简单函数调用获取列表相比,每个都会产生大量开销。 ,函数使用For循环创建值。

msvcyc is correct in pointing out that the parameter is expecting a string value, but the function is returning an array. I changed the return type to Array as suggested by prashant sable, but the select list is still grayed out, it does not work. And coldice is correct in saying that the access modifier should be Public.

msvcyc指出该参数需要一个字符串值,但该函数返回一个数组是正确的。我按照prashant sable的建议将返回类型更改为Array,但是选择列表仍然是灰色的,它不起作用。并且冷却是正确的,说访问修饰符应该是公共的。

In my digging around I found an article by James Kovac from 2005 that pointed out why this is not possible. The Parameters class has a get method, but no set method. In the VS 2008 object browser for SSRS 2008 the object name has changed, but it still does not contain a set method (see Microsoft.ReportingServices.Interfaces.IParameter.Name or .Value).

在我的挖掘中,我发现James Kovac从2005年开始发表一篇文章,指出为什么这是不可能的。 Parameters类有一个get方法,但没有set方法。在SSRS 2008的VS 2008对象浏览器中,对象名称已更改,但它仍然不包含set方法(请参阅Microsoft.ReportingServices.Interfaces.IParameter.Name或.Value)。

My current workaround is to just hard code the list of values, but if your value list needs to be dynamic then your only choices are database queries, web services, or custom assemblies. I think the easiest workaround of these three is to get the values from the database engine, as suggested by oleksiy.t, as long as you can write a query to return the value list you want. Your list of integers, or my list of time intervals, would both be easy queries to write. Otherwise you will need to use one of the other two workarounds.

我目前的解决方法是硬编码值列表,但如果您的值列表需要是动态的,那么您唯一的选择是数据库查询,Web服务或自定义程序集。我认为这三个最简单的解决方法是从oleksiy.t建议的数据库引擎中获取值,只要您可以编写查询以返回所需的值列表。您的整数列表或我的时间间隔列表都可以轻松查询。否则,您将需要使用另外两种解决方法之一。

#6


1  

I checked your code. The only thing that's wrong is your function returns String(). When I changed your method signature to return Array, it worked fine, in my report.

我检查了你的代码。唯一错误的是你的函数返回String()。当我更改您的方法签名以返回Array时,它在我的报告中工作正常。

Change the signature to Function GetPeriods() As Array

将签名更改为Function GetPeriods()As Array

#7


0  

Everything I've seen requires parameters and their respective settings to be part of the RDL.

我见过的所有内容都需要参数及其各自的设置才能成为RDL的一部分。

That being said, if you're going to "hardcode" the values, you could create a dataset just for the report, perhaps in XML, or if it needs to be programmatically driven, do it in a web service.

话虽这么说,如果您要对这些值进行“硬编码”,您可以为报表创建一个数据集,可能是XML,或者如果需要以编程方式驱动,请在Web服务中执行。