如下代碼演示如何使用連接字符串設置 ActiveConnection 屬性。該代碼假定在名為 Servername 的 OLAP 服務器上有一個名為 Bob Video Store 的數(shù)據(jù)源,并且使用的是 MSOLAP 數(shù)據(jù)提供者。
Dim cat As New ADOMD.Catalog
cat.ActiveConnection = "Data Source=Servername;" + _
"Initial Catalog=Bobs Video Store;Provider=msolap;"
如下代碼也演示如何使用連接字符串設置 ActiveConnection 屬性。但是,該代碼使用 MSOLAP 數(shù)據(jù)提供者連接到本地立方文件,而不是連接到名為 Servername 的 OLAPT 服務器。
Dim cat As New ADOMD.Catalog
cat.ActiveConnection = _
"Location=C:\MSDASDK\samples\oledb\olap\data\BobsVid.cub;" + _
"Provider=msolap;"
如下代碼演示如何將 ActiveConnection 屬性設置為標準的ADO Connection 對象。要使用 ADO Connection 對象,必須在工程中引用 ADO 類型庫。
Dim cnn As New ADODB.Connection
Dim cat As New ADOMD.Catalog
Cnn.Open "Data Source=Servername;" + _
"Initial Catalog=Bobs Video Store;Provider=msolap;"
Set cat.ActiveConnection = cnn
注意 在將 ActiveConnection 屬性設置為 Connection 對象時,記住要使用 Set 關鍵字。如果沒有使用 Set 關鍵詞,其結果是將 ActiveConnection 屬性設置成 Connection 的默認屬性:ConnectionString。代碼將會工作,但將創(chuàng)建與數(shù)據(jù)源的其他連接,最終得到相反的結果。