从 ADO MD 迁移到 ADOMD.NET

ADOMD.NET 库类似于 ActiveX 多维数据对象 (ADO MD) 库,它是 ActiveX 数据对象 (ADO) 库的扩展,该库用于访问基于组件对象模型 (COM) 的客户端应用程序中的多维数据。 ADO MD 提供了从非托管语言(如 c + + 和 Microsoft Visual Basic)轻松访问多维数据的权限。 通过 ADOMD.NET,可以轻松访问来自托管语言(例如 microsoft c # 和 microsoft Visual Basic .net)的多维和数据挖掘) 数据的分析 (。 此外,ADOMD.NET 还提供了增强的元数据对象模型。

将现有的客户端应用程序从 ADO MD 迁移到 ADOMD.NET 很容易,但其中存在多个有关迁移的重要差异:

提供对客户端应用程序的连接和数据访问

ADO MD ADOMD.NET
需要对 Adodb.dll 和 Adomd.dll 的引用。 需要对单个 Microsoft.AnalysisServices.AdomdClient.dll 的引用。

AdomdConnection除了访问元数据,类还提供连接支持。

检索多维对象的元数据

ADO MD ADOMD.NET
使用目录类。 Cubes使用的属性 AdomdConnection

运行查询并返回单元集对象

ADO MD ADOMD.NET
使用单元集类。 使用 AdomdCommand 类。

访问用于显示单元集的元数据

ADO MD ADOMD.NET
使用 Position 类。 Set使用和 Tuple 对象。

注意

Position类支持向后兼容。

检索挖掘模型元数据
ADO MD 中没有可用的类。 在 ADOMD.NET 中,使用数据挖掘集合之一:

为了突出显示这些差异,下面的迁移示例将现有的 ADO MD 应用程序与等效的 ADOMD.NET 应用程序进行了比较。

查看迁移示例

本节中显示的现有 ADO MD 和等效的 ADOMD.NET 代码示例执行同一组操作:创建连接、运行多维表达式 (MDX) 语句以及检索元数据和数据。 但是,这两组代码不使用相同的对象来执行上述任务。

现有的 ADO MD 代码

以下从 ADO MD 2.8 文档中提取的代码示例是用 Microsoft Visual Basic®6.0 编写的,并使用 ADO MD 演示如何连接到 Microsoft SQL Server 数据源并进行查询。 此 ADO MD 示例使用以下对象:

  • 目录 对象创建连接。

  • 使用 单元格集 对象 (MDX) 语句运行多维表达式。

  • 位置 对象检索元数据和数据,该对象从 单元集 对象中检索。

Private Sub cmdCellSettoDebugWindow_Click()  
Dim cat As New ADOMD.Catalog  
Dim cst As New ADOMD.Cellset  
Dim i As Integer  
Dim j As Integer  
Dim k As Integer  
Dim strServer As String  
Dim strSource As String  
Dim strColumnHeader As String  
Dim strRowText As String  
  
On Error GoTo Error_cmdCellSettoDebugWindow_Click  
Screen.MousePointer = vbHourglass  
'*-----------------------------------------------------------------------  
'* Set server to local host.  
'*-----------------------------------------------------------------------  
    strServer = "LOCALHOST"  
  
'*-----------------------------------------------------------------------  
'* Set MDX query string source.  
'*-----------------------------------------------------------------------  
    strSource = strSource & "SELECT "  
    strSource = strSource & "{[Measures].members} ON COLUMNS,"  
    strSource = strSource & _  
        "NON EMPTY [Store].[Store City].members ON ROWS"  
    strSource = strSource & " FROM Sales"  
  
'*-----------------------------------------------------------------------  
'* Set active connection.  
'*-----------------------------------------------------------------------  
        cat.ActiveConnection = "Data Source=" & strServer & _  
            ";Provider=msolap;"  
  
'*-----------------------------------------------------------------------  
'* Set cellset source to MDX query string.  
'*-----------------------------------------------------------------------  
        cst.Source = strSource  
  
'*-----------------------------------------------------------------------  
'* Set cellset active connection to current connection  
'*-----------------------------------------------------------------------  
    Set cst.ActiveConnection = cat.ActiveConnection  
  
'*-----------------------------------------------------------------------  
'* Open cellset.  
'*-----------------------------------------------------------------------  
    cst.Open  
  
'*-----------------------------------------------------------------------  
'* Allow space for row header text.  
'*-----------------------------------------------------------------------  
strColumnHeader = vbTab & vbTab & vbTab & vbTab & vbTab & vbTab  
  
'*-----------------------------------------------------------------------  
'* Loop through column headers.  
'*-----------------------------------------------------------------------  
       For i = 0 To cst.Axes(0).Positions.Count - 1  
            strColumnHeader = strColumnHeader & _  
                cst.Axes(0).Positions(i).Members(0).Caption & vbTab & _  
                    vbTab & vbTab & vbTab  
       Next  
       Debug.Print vbTab & strColumnHeader & vbCrLf  
  
'*-----------------------------------------------------------------------  
'* Loop through row headers and provide data for each row.  
'*-----------------------------------------------------------------------  
        strRowText = ""  
        For j = 0 To cst.Axes(1).Positions.Count - 1  
            strRowText = strRowText & _  
                cst.Axes(1).Positions(j).Members(0).Caption & vbTab & _  
                    vbTab & vbTab & vbTab  
            For k = 0 To cst.Axes(0).Positions.Count - 1  
                strRowText = strRowText & cst(k, j).FormattedValue & _  
                    vbTab & vbTab & vbTab & vbTab  
            Next  
            Debug.Print strRowText & vbCrLf  
            strRowText = ""  
        Next  
  
    Screen.MousePointer = vbDefault  
Exit Sub  
  
Error_cmdCellSettoDebugWindow_Click:  
   Beep  
   Screen.MousePointer = vbDefault  
   MsgBox "The following error has occurred:" & vbCrLf & _  
      Err.Description, vbCritical, " Error!"  
   Exit Sub  
End Sub  

等效的 ADOMD.NET 代码

下面的示例是用 Visual Basic .NET 编写的,并使用 ADOMD.NET 演示了如何执行与早期的 Visual Basic 6.0 示例相同的操作。 下面的示例与前面显示的 ADO MD 示例之间最主要差异是用于执行这些操作的对象。 ADOMD.NET 示例使用下列对象:

  • 使用 AdomdConnection 对象创建连接。

  • 使用 AdomdCommand 对象运行 MDX 语句。

  • 从集对象检索元数据和数据,该 对象从 单元 集对象中检索。

Private Sub DisplayCellSetInOutputWindow()  
    Dim conn As AdomdConnection  
    Dim cmd As AdomdCommand  
    Dim cst As CellSet  
    Dim i As Integer  
    Dim j As Integer  
    Dim k As Integer  
    Dim strServer As String = "LOCALHOST"  
    Dim strSource As String = "SELECT [Measures].members ON COLUMNS, " & _  
        "NON EMPTY [Store].[Store City].members ON ROWS FROM SALES"  
    Dim strOutput As New System.IO.StringWriter  
  
    '*-----------------------------------------------------------------------  
    '* Open connection.  
    '*-----------------------------------------------------------------------  
    Try  
        ' Create a new AdomdConnection object, providing the connection  
        ' string.  
        conn = New AdomdConnection("Data Source=" & strServer & _  
        ";Provider=msolap;")  
        ' Open the connection.  
        conn.Open()  
    Catch ex As Exception  
        Throw New ApplicationException( _  
            "An error occurred while connecting.")  
    End Try  
  
    Try  
    '*-----------------------------------------------------------------------  
    '* Open cellset.  
    '*-----------------------------------------------------------------------  
        ' Create a new AdomdCommand object, providing the MDX query string.  
        cmd = New AdomdCommand(strSource, conn)  
        ' Run the command and return a CellSet object.  
        cst = cmd.ExecuteCellSet()  
  
    '*-----------------------------------------------------------------------  
    '* Concatenate output.  
    '*-----------------------------------------------------------------------  
  
    ' Include spacing to account for row headers.  
    strOutput.Write(vbTab, 6)  
  
    ' Iterate through the first axis of the CellSet object and  
    ' retrieve column headers.  
    For i = 0 To cst.Axes(0).Set.Tuples.Count - 1  
        strOutput.Write(cst.Axes(0).Set.Tuples(i).Members(0).Caption)  
        strOutput.Write(vbTab, 4)  
    Next  
    strOutput.WriteLine()  
  
    ' Iterate through the second axis of the CellSet object and  
    ' retrieve row headers and cell data.  
    For j = 0 To cst.Axes(1).Set.Tuples.Count - 1  
        ' Append the row header.  
        strOutput.Write(cst.Axes(1).Set.Tuples(j).Members(0).Caption)  
        strOutput.Write(vbTab, 4)  
  
        ' Append the cell data for that row.  
        For k = 0 To cst.Axes(0).Set.Tuples.Count - 1  
            strOutput.Write(cst.Cells(k, j).FormattedValue)  
            strOutput.Write(vbTab, 4)  
        Next  
        strOutput.WriteLine()  
    Next  
  
    ' Display the output.  
    Debug.WriteLine(strOutput.ToString)  
  
    '*-----------------------------------------------------------------------  
    '* Release resources.  
    '*-----------------------------------------------------------------------  
        conn.Close()  
    Catch ex As Exception  
        ' Ignore or handle errors.  
    Finally  
        cst = Nothing  
        cmd = Nothing  
        conn = Nothing  
    End Try  
End Sub