如何使用ADOMD执行OLAP操作
我所用的OLAP是微软的Analysis Services,后台用SQL Server。用VB.NET实现。
具体做法是先选择菜单“项目”--》“添加引用”,然后在COM组件的标签页中找到ADOMD,添加即可。
1. 连接服务器
Dim conn As New ADODB.Connection
Dim cat As New ADOMD.Catalog
Dim cubedef As ADOMD.CubeDef
Dim dimension As ADOMD.Dimension
Dim hier As ADOMD.Hierarchy
Dim level As ADOMD.Level
Dim member As ADOMD.Member
conn.ConnectionString = "Datasource=LocalHost; Provider=msolap; Initial Catalog=community;"
conn.Open()
cat.ActiveConnection = conn
cubedef = cat.CubeDefs("Job")
dimension = cubedef.Dimensions.Item("JobState")
hier = dimension.Hierarchies.Item(0)
level = hier.Levels.Item("Jobstate Name")
2. 执行MDX语句
Dim strMDX As String
Dim cellset As New ADOMD.Cellset
Dim axis As ADOMD.Axis
Dim pos As ADOMD.Position
strMDX = "SELECT {[Community].[Community Name].Members} on COLUMNS FROM [Job] WHERE ([JobState].[Jobstate Name].[" & RowName & "])"
cellset.Open(strMDX, conn)
axis = cellset.Axes(0)
Dim count As Integer
count = axis.Positions.Count
GetRowData = Array.CreateInstance(GetType(Integer), count)
Dim i As Integer
i = 0
While i < count
GetRowData.SetValue(CInt(cellset.Item(i).Value), i)
i = i + 1
End While
此处特别注意执行MDX语句的时候,要使用这种形式:
cellset.Open(strMDX, conn)
MSDN上面的例子是先设置CellSet的属性,然后再open,这种方法执行的时候会报错说“类型不匹配”的。