将 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)
Table 对象 (ADOX)
表集合 (ADOX)
Type 属性 (表) (ADOX)