连接的 Close 方法、表 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)
目录对象 (ADOX)
列对象 (ADOX)
列集合 (ADOX)
表对象 (ADOX)
表集合 (ADOX)
Type 属性(表)(ADOX)