--- title: "Open and Close Methods Example (VBScript) | Microsoft Docs" ms.prod: sql ms.prod_service: connectivity ms.technology: connectivity ms.custom: "" ms.date: "01/19/2017" ms.reviewer: "" ms.topic: conceptual dev_langs: - "VB" helpviewer_keywords: - "Close method [ADO], VBScript example" - "Open method [ADO], VBScript example" ms.assetid: 66eca011-e258-4d8f-bd67-e017bcf0871b author: MightyPen ms.author: genemi --- # Open and Close Methods Example (VBScript) This example uses the [Open](../../../ado/reference/ado-api/open-method-ado-recordset.md) and [Close](../../../ado/reference/ado-api/close-method-ado.md) methods on both [Recordset](../../../ado/reference/ado-api/recordset-object-ado.md) and [Connection](../../../ado/reference/ado-api/connection-object-ado.md) objects that have been opened. Use the following example in an Active Server Page (ASP). Use **Find** to locate the file Adovbs.inc and place it in the directory you plan to use. Cut and paste the following code into Notepad or another text editor, and save it as **OpenVBS.asp**. You can view the result in any browser. ``` <%@ Language=VBScript %> <%' use this meta tag instead of adovbs.inc%> ADO Open Method

ADO Open Method

<% ' to integrate/test this code replace the ' Data Source value in the Connection string%> <% ' connection and recordset variables Dim Cnxn, strCnxn Dim rsCustomers, strSQLCustomers Dim rsProducts, strSQLProducts ' open connection Set Cnxn = Server.CreateObject("ADODB.Connection") strCnxn = "Provider='sqloledb';Data Source=" & _ Request.ServerVariables("SERVER_NAME") & ";" & _ "Integrated Security='SSPI';Initial Catalog='Northwind';" Cnxn.Open strCnxn ' create and open first Recordset using Connection - execute Set rsCustomers = Server.CreateObject("ADODB.Recordset") strSQLCustomers = "SELECT CompanyName, ContactName, City FROM Customers" Set rsCustomers = Cnxn.Execute(strSQLCustomers) ' create and open second Recordset using recordset - open Set rsProducts = Server.CreateObject("ADODB.Recordset") strSQLProducts = "SELECT ProductName, UnitPrice FROM Products" rsProducts.Open strSQLProducts, Cnxn, adOpenDynamic, adLockPessimistic, adCmdText %> <% Do Until rsCustomers.EOF %> <%rsCustomers.MoveNext Loop %>
Company Name Contact Name City
<%=rsCustomers("CompanyName")%> <%=rsCustomers("ContactName")%> <%=rsCustomers("City")%>

<% Do Until rsProducts.EOF %> <%rsProducts.MoveNext Loop ' clean up If rsProducts.State = adStateOpen then rsProducts.Close End If If rsCustomers.State = adStateOpen then rsCustomers.Close End If If Cnxn.State = adStateOpen then Cnxn.Close End If Set rsProducts = Nothing Set rsCustomers = Nothing Set Cnxn = Nothing %>
Product Name Unit Price
<%=rsProducts("ProductName")%> <%=rsProducts("UnitPrice")%>
``` ## See Also [Close Method (ADO)](../../../ado/reference/ado-api/close-method-ado.md) [Connection Object (ADO)](../../../ado/reference/ado-api/connection-object-ado.md) [Open Method (ADO Connection)](../../../ado/reference/ado-api/open-method-ado-connection.md) [Open Method (ADO Recordset)](../../../ado/reference/ado-api/open-method-ado-recordset.md) [Recordset Object (ADO)](../../../ado/reference/ado-api/recordset-object-ado.md)