Connection Close メソッド、Table Type プロパティの例 (VB)

ActiveConnection プロパティを Nothing に設定すると、カタログへの接続が閉じられます。 関連付けられているコレクションは空になります。 カタログ内のスキーマ オブジェクトから作成されたオブジェクトは、すべて孤立します。 キャッシュされているそれらのオブジェクトのプロパティはすべて引き続き使用できますが、プロバイダーへの呼び出しを必要とするプロパティの読み取りは失敗します。

' BeginCloseConnectionVB  
Sub Main()  
    On Error GoTo CloseConnectionByNothingError  
  
    Dim cnn As New ADODB.Connection  
    Dim cat As New ADOX.Catalog  
    Dim tbl As ADOX.Table  
  
    cnn.Open "Provider='Microsoft.Jet.OLEDB.4.0';" & _  
        "Data Source= 'Northwind.mdb';"  
    Set cat.ActiveConnection = cnn  
    Set tbl = cat.Tables(0)  
    Debug.Print tbl.Type    ' Cache tbl.Type info  
    Set cat.ActiveConnection = Nothing  
    Debug.Print tbl.Type    ' tbl is orphaned  
    ' Previous line will succeed if this info was cached.  
    Debug.Print tbl.Columns(0).DefinedSize  
    ' Previous line will fail if this info has not been cached.  
  
    'Clean up.  
    cnn.Close  
    Set cat = Nothing  
    Set cnn = Nothing  
    Exit Sub  
  
CloseConnectionByNothingError:  
    Set cat = Nothing  
  
    If Not cnn Is Nothing Then  
        If cnn.State = adStateOpen Then cnn.Close  
    End If  
    Set cnn = Nothing  
  
    If Err <> 0 Then  
        MsgBox Err.Source & "-->" & Err.Description, , "Error"  
    End If  
End Sub  
' EndCloseConnectionVB  

カタログを開くために使用された Connection オブジェクトを閉じることは、ActiveConnection プロパティを Nothing に設定するのと同じ効果があります。

Attribute VB_Name = "Connection"  

参照

ActiveConnection プロパティ (ADOX)
Catalog オブジェクト (ADOX)
Column オブジェクト (ADOX)
Columns コレクション (ADOX)
Table オブジェクト (ADOX)
Tables コレクション (ADOX)
Type プロパティ (テーブル) (ADOX)