Partager via


Close, méthode de l’objet Connection, Type (exemple de propriété de l’objet Table) (VB)

La définition de la propriété ActiveConnection sur Nothing doit normalement fermer la connexion au catalogue. Les collections associées sont vides. Tous les objets créés à partir d’objets de schéma dans le catalogue deviendront orphelins. Toutes les propriétés sur ces objets mis en cache demeurent disponibles, mais toute tentative de lecture des propriétés nécessitant un appel au fournisseur échouera.

' 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  

La fermeture d’un objet Connection utilisé pour ouvrir le catalogue doit avoir le même effet que la définition de la propriété ActiveConnection sur Nothing.

Attribute VB_Name = "Connection"  

Voir aussi

ActiveConnection, propriété (ADOX)
Catalog, objet (ADOX)
Column, objet (ADOX)
Columns, collection (ADOX)
Table, objet (ADOX)
Tables, collection (ADOX)
Type, propriété (table) (ADOX)