Connections收集 (DAO)
适用于:Access 2013、Office 2013
注意
Microsoft Access 2013 中不支持 ODBCDirect 工作区。 如果要在不使用 Microsoft Access 数据库引擎的情况下访问外部数据源,请使用 ADO。
Connections 集合包含 Workspace 对象的当前 Connection 对象。 一个 Connections 集合,包含 Workspace 对象的当前 Connection 对象(仅适用于 ODBCDirect 工作区)。
备注
打开 Connection 对象时,该对象会自动追加到 Workspace 的 Connections 集合中。 使用 Close 方法关闭 Connection 对象时,会从 Connections 集合中删除该对象。 在关闭连接之前,应先关闭连接中所有打开的 Recordset 对象。
打开 Connection 对象的同时,会创建一个相应的 Database 对象,该对象追加到同一 Workspace 中的 Databases 集合中,反之亦然。 同样,关闭 Connection 时,即从 Databases 集合中删除相应的 Database,依此类推。
Connection 的 Name 属性设置是一个指定数据库文件路径的字符串。 若要按照序号或 Name 属性设置来引用集合中的 Connection 对象,可以使用下列任何一种语法形式:
Connections (0)
Connections (“name”)
Connections![name]
注意
[!注释] 可以多次打开同一数据源,这样会在 Connections 集合中生成重复的名称。 应当将 Connection 对象分配给对象变量,并通过变量名来引用它们。
示例
以下示例通过打开一个 Database 对象和两个 ODBCDirect Connection 对象并列出可用于每个对象的属性,来演示 Connection 对象和 Connections 集合。
Sub ConnectionObjectX()
Dim wrkAcc as Workspace
Dim dbsNorthwind As Database
Dim wrkODBC As Workspace
Dim conPubs As Connection
Dim conPubs2 As Connection
Dim conLoop As Connection
Dim prpLoop As Property
' Open a Database object.
Set wrkAcc = CreateWorkspace("NewWorkspace", _
"admin", "", dbUseJet)
Set dbsNorthwind = wrkAcc.OpenDatabase("Northwind.mdb")
' Create ODBCDirect Workspace object and open Connection
' objects.
Set wrkODBC = CreateWorkspace("NewODBCWorkspace", _
"admin", "", dbUseODBC)
' Note: The DSNs referenced below must be configured to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
Set conPubs = wrkODBC.OpenConnection("Connection1", , , _
"ODBC;DATABASE=pubs;DSN=Publishers")
Set conPubs2 = wrkODBC.OpenConnection("Connection2", , _
True, "ODBC;DATABASE=pubs;DSN=Publishers")
Debug.Print "Database properties:"
With dbsNorthwind
' Enumerate Properties collection of Database object.
For Each prpLoop In .Properties
On Error Resume Next
Debug.Print " " & prpLoop.Name & " = " & _
prpLoop.Value
On Error GoTo 0
Next prpLoop
End With
' Enumerate the Connections collection.
For Each conLoop In wrkODBC.Connections
Debug.Print "Connection properties for " & _
conLoop.Name & ":"
With conLoop
' Print property values by explicitly calling each
' Property object; the Connection object does not
' support a Properties collection.
Debug.Print " Connect = " & .Connect
' Property actually returns a Database object.
Debug.Print " Database[.Name] = " & _
.Database.Name
Debug.Print " Name = " & .Name
Debug.Print " QueryTimeout = " & .QueryTimeout
Debug.Print " RecordsAffected = " & _
.RecordsAffected
Debug.Print " StillExecuting = " & _
.StillExecuting
Debug.Print " Transactions = " & .Transactions
Debug.Print " Updatable = " & .Updatable
End With
Next conLoop
dbsNorthwind.Close
conPubs.Close
conPubs2.Close
wrkAcc.Close
wrkODBC.Close
End Sub
以下示例使用采用不同参数的 OpenConnection 方法打开三个不同的 Connection 对象。
Sub OpenConnectionX()
Dim wrkODBC As Workspace
Dim conPubs As Connection
Dim conPubs2 As Connection
Dim conPubs3 As Connection
Dim conLoop As Connection
' Create ODBCDirect Workspace object.
Set wrkODBC = CreateWorkspace("NewODBCWorkspace", _
"admin", "", dbUseODBC)
' Open Connection object using supplied information in
' the connect string. If this information were
' insufficient, you could trap for an error rather than
' go to an ODBC Driver Manager dialog box.
MsgBox "Opening Connection1..."
' Note: The DSN referenced below must be set to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
Set conPubs = wrkODBC.OpenConnection("Connection1", _
dbDriverNoPrompt, , _
"ODBC;DATABASE=pubs;DSN=Publishers")
' Open read-only Connection object based on information
' you enter in the ODBC Driver Manager dialog box.
MsgBox "Opening Connection2..."
Set conPubs2 = wrkODBC.OpenConnection("Connection2", _
dbDriverPrompt, True, "ODBC;DSN=Publishers;")
' Open read-only Connection object by entering only the
' missing information in the ODBC Driver Manager dialog
' box.
MsgBox "Opening Connection3..."
Set conPubs3 = wrkODBC.OpenConnection("Connection3", _
dbDriverCompleteRequired, True, _
"ODBC;DATABASE=pubs;DSN=Publishers;")
' Enumerate the Connections collection.
For Each conLoop In wrkODBC.Connections
Debug.Print "Connection properties for " & _
conLoop.Name & ":"
With conLoop
' Print property values by explicitly calling each
' Property object; the Connection object does not
' support a Properties collection.
Debug.Print " Connect = " & .Connect
' Property actually returns a Database object.
Debug.Print " Database[.Name] = " & _
.Database.Name
Debug.Print " Name = " & .Name
Debug.Print " QueryTimeout = " & .QueryTimeout
Debug.Print " RecordsAffected = " & _
.RecordsAffected
Debug.Print " StillExecuting = " & _
.StillExecuting
Debug.Print " Transactions = " & .Transactions
Debug.Print " Updatable = " & .Updatable
End With
Next conLoop
conPubs.Close
conPubs2.Close
conPubs3.Close
wrkODBC.Close
End Sub