我试图从SSAS Databases检索SSAS Cubes及其Server的列表,我在网上找到了这段代码(代码是VB.NET,但是问题在C#中也是一样的:
Private Function GetCubesBad() As DatabaseCollection
Dim databaseCollection As DatabaseCollection
Using server As New Server
server.Connect(String.Format("Data Source={0};Connect Timeout=30", "MYSERVER"))
databaseCollection = server.Databases
server.Disconnect()
End Using
Return databaseCollection
End Function
但这只返回一个列表数据库,不包含任何多维数据集。
然后我稍微摆弄了一下,最终让它像这样工作:
Private Function GetCubesGood() As List(Of Cube)
Dim cubeCollection As New List(Of Cube)
Using server As New Server
server.Connect(String.Format("Data Source={0};Connect Timeout=30", "MYSERVER"))
For Each database As Database In server.Databases
For Each cube As Cube In database.Cubes
cubeCollection.Add(cube)
Next
Next
server.Disconnect()
End Using
Return cubeCollection
End Function
现在我很好奇为什么第一个功能不起作用......
编辑:
当我像这样修改GetCubesBad
函数时:
databaseCollection = server.Databases
For Each database As Database In databaseCollection
Dim dummy = database.Cubes
Next
它就像一个魅力。因此,在连接到服务器时,似乎需要为每个数据库调用一次Cubes属性,否则Cubes属性保持为空。
这种行为有原因吗?