Condividi tramite


Migrazione da ADO MD ad ADOMD.NET

La libreria ADOMD.NET è analoga alla libreria ADO MD (ActiveX Data Objects Multidimensional), un'estensione della libreria ADO (ActiveX Data Objects) utilizzata per accedere ai dati multidimensionali nelle applicazioni client basate sul modello COM (Component Object Model). ADO MD consente di accedere facilmente ai dati multidimensionali da linguaggi non gestiti come C++ e Microsoft Visual Basic. ADOMD.NET fornisce un facile accesso ai dati analitici (sia multidimensionali che data mining) da linguaggi gestiti come Microsoft C# e Microsoft Visual Basic .NET. In ADOMD.NET è disponibile inoltre un modello a oggetti per metadati notevolmente migliorato.

La migrazione di applicazioni client esistenti da ADO MD ad ADOMD.NET è semplice da eseguire, ma sono presenti diverse importanti differenze relative alla migrazione:

Per fornire connettività e accesso ai dati alle applicazioni client

ADO MD ADOMD.NET
Necessità di riferimenti sia ad Adodb.dll che ad Adomd.dll. Necessità di un unico riferimento a Microsoft.AnalysisServices.AdomdClient.dll.

La AdomdConnection classe fornisce supporto per la connettività, oltre all'accesso ai metadati.

Per recuperare metadati per gli oggetti multidimensionali

ADO MD ADOMD.NET
Usare la classe Catalog. Utilizzare la Cubes proprietà dell'oggetto AdomdConnection.

Per eseguire query e restituire oggetti del set di celle

ADO MD ADOMD.NET
Usare la classe CellSet. Usare la classe AdomdCommand.

Per accedere ai metadati utilizzati per visualizzare un set di celle

ADO MD ADOMD.NET
Usare la classe Position. Usare gli oggetti Set e Tuple .

Nota

La classe Position è supportata per la compatibilità con le versioni precedenti.

Per recuperare metadati del modello di data mining
In ADO MD non sono disponibili classi. In ADOMD.NET usare una delle raccolte data mining seguenti:

Per evidenziare tali differenze, negli esempi di migrazione seguenti viene eseguito il confronto di un'applicazione ADO MD esistente con un'applicazione ADOMD.NET equivalente.

Analisi di un esempio di migrazione

Sebbene nell'esempio di codice ADO MD esistente e nell'esempio ADOMD.NET equivalente illustrati in questa sezione venga eseguito lo stesso set di azioni, ovvero creazione di una connessione, esecuzione di un'istruzione MDX (Multidimensional Expressions) e recupero di metadati e dati, negli esempi non vengono utilizzati gli stessi oggetti per eseguire tali attività.

Codice ADO MD esistente

L'esempio di codice seguente, tratto dalla documentazione di ADO MD 2.8, è scritto in Microsoft Visual Basic ® 6.0 e usa ADO MD per illustrare come connettersi a un'origine dati Microsoft SQL Server ed eseguire query. Nell'esempio ADO MD vengono utilizzati i seguenti oggetti:

  • Crea una connessione da un oggetto Catalog .

  • Esegue l'istruzione MDX (Multidimensional Expressions) usando l'oggetto Cellset .

  • Recupera i metadati e i dati dall'oggetto Position , recuperati dall'oggetto Cellset .

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  

Codice ADOMD.NET equivalente

Nell'esempio seguente, scritto in Visual Basic .NET utilizzando ADOMD.NET, viene illustrato come eseguire le stesse azioni dell'esempio scritto in Visual Basic 6.0 precedente. La differenza principale tra l'esempio seguente e l'esempio ADO MD precedente è costituita dagli oggetti utilizzati per eseguire le azioni. Nell'esempio ADOMD.NET vengono utilizzati i seguenti oggetti:

  • Crea una connessione con un oggetto AdomdConnection .

  • Esegue l'istruzione MDX usando un oggetto AdomdCommand .

  • Recupera i metadati e i dati dall'oggetto Set , recuperati dall'oggetto Cellset .

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