或者,可以使用 Recordset.Open 隐式建立连接并在单个操作中针对该连接发出命令。 例如,在 Visual Basic 中:
Dim oRs As ADODB.Recordset
Dim sConn As String
Dim sSQL as String
sConn = "Provider='SQLOLEDB';Data Source='MySqlServer';" & _ "Initial Catalog='Northwind';Integrated Security='SSPI';"
sSQL = "SELECT ProductID, ProductName, CategoryID, UnitPrice " & _
"FROM Products"
' Create and Open the Recordset object.
Set oRs = New ADODB.Recordset
oRs.CursorLocation = adUseClient
oRs.Open sSQL, sConn, adOpenStatic, _
adLockBatchOptimistic, adCmdText
MsgBox oRs.RecordCount
oRs.MarshalOptions = adMarshalModifiedOnly
' Disconnect the Recordset.
Set oRs.ActiveConnection = Nothing
oRs.Close
Set oRs = Nothing
请注意,oRs.Open 使用连接字符串(sConn)来替代 Connection 对象(oConn),作为其 ActiveConnection 参数的值。 此外,还可以通过在 Recordset 对象上设置 CursorLocation 属性来强制实施客户端游标类型。 同样,将此与 HelloData 示例形成鲜明对比。