A dataset in Reporting Services is not the same type of object as an ADO.Net dataset. A report dataset is an internal object managed by the SSRS runtime (it's actually derived from a DataReader object) and not an XML structure containing datatables, etc. and cannot be passed into the report's custom code.
我们在SSRS中创建了DataSet,然后试图通过Customer Code来访问该DataSet。在一番查询之后,发现似乎没有办法直接在Customer Code中调用DataSet...
然而,Customer Code是可以访问Parameter的,而Parameter又和DataSet能够关联,于是就有了如下的方案来实现Customer Code 遍历DataSet。
1,创建DataSet,命名为Test_DataSet
创建测试用例,该DataSet有2列,分别为Id和Name
Select 1 As 'Id',1000 As 'Name' Union All Select 2 As 'Id',2000 As 'Name'
2,创建Parameter,命名为Test_Parameter
在General页签,选择Data Type为Text,Allow multiple values为True,Hidden为True
在Availabe Values页签,选择Get values from a query,DataSet选择Test_DataSet,Value Filed选择Id,Label Field选择Name
在Default Values页签,选择Get vaules from a query,DataSet选择Test_DateSet,Value Field选择Id
在Advanced页签,选择Never refresh
3,选中Report,查看属性,在Code中输入如下Vb Code
这实现了根据输入的Id,遍历DataSet得到对应的Name的效果
Public Function GetValue(Id As Integer) As String Dim i As Integer i=0 For i = 0 to Report.Parameters!Test_Parameter.Count() - 1 If Report.Parameters!Test_Parameter.Value(i) = Id Then GetValue = Report.Parameters!Test_Parameter.Label(i) Exit For End If Next i End Function
4,在Report中,对应的单元格中,使用如下Expression进行调用
=Code.VerValue(1)
Preview Report,该单元格中显示值1000