The AdomdConnection represents a connection with an analytical data source (for example, a connection to an instance of Microsoft SQL Server 2005 Analysis Services (SSAS)). Each connection is associated with an XML for Analysis session. If an AdomdConnection goes out of scope, the connection is not closed. To close the connection, you must explicitly call the Close or Dispose method of the AdomdConnection. If the Close or Dispose method is not called, the session associated with the AdomdConnection remains active and can be reused by another AdomdConnection. For more information about managing sessions, see Working with Connections and Sessions in ADOMD.NET and SessionID.

Public Function Connect(Optional ByVal ServerID As String = "localhost", _
Optional ByVal DatabaseID As String = "", _
Optional ByVal UserID As String = "", _
Optional ByVal Password As String = "") As AdomdConnection
Dim strConnectionString As String
Dim objConnection As AdomdConnection
Try
' First, build the connection string.
'
' The ConnectionString property consists of
' key/value pairs seperated by semicolons.
' - The format, as a regular expression, should be:
' (<Key>=<Value>)*([[;]/*(<Key>=<Value>)])
' The connection string can take "Datasource", "Data Source",
' or "Location" for this parameter.
If ServerID <> "" Then
strConnectionString &= "Data Source=" & ServerID & ";"
End If
' The connection string can take "Catalog" or "Initial Catalog"
' for this parameter.
If DatabaseID <> "" Then
strConnectionString &= "Initial Catalog=" & DatabaseID & ";"
End If
' The connection string can take "UID", "User ID",
' "Authenticated User", or "UserName" for this parameter.
If UserID <> "" Then
strConnectionString &= "User ID=" & UserID & ";"
End If
' The connection string can take "PWD" or "Password"
' for this parameter.
If Password <> "" Then
strConnectionString &= "Password=" & Password & ";"
End If
' Get a reference to an AdomdConnection object.
objConnection = New AdomdConnection()
' Assign the connection string and try to
' open a connection.
objConnection.ConnectionString = strConnectionString
objConnection.Open()
Catch ex As AdomdConnectionException
' The connection could not be opened or was disconnected.
' This error can occur at any time, if the provider is
' disconnected from the server.
Debug.WriteLine(ex)
Catch ex As AdomdErrorResponseException
' A response is received from a provider that indicates an error.
Debug.WriteLine(ex)
Catch ex As AdomdUnknownResponseException
' A response has been returned from the provider that
' was not understood.
Debug.WriteLine(ex)
Catch ex As AdomdCacheExpiredException
' A cached version of an ADOMD.NET object is no longer valid.
' This error is typically raised when reviewing metadata.
Debug.WriteLine(ex)
Catch ex As AdomdException
' Any other error raised by ADOMD.NET.
Debug.WriteLine(ex)
Catch ex As Exception
' Any other error that occurs.
Debug.WriteLine(ex)
Finally
Debug.Write("The connection ")
If objConnection.State = ConnectionState.Open Then
Debug.WriteLine("is connected!")
Else
Debug.WriteLine("failed to connect!")
End If
End Try
' Return the AdomdConnection object.
Return objConnection
End Function