--- title: "Execute, Requery, and Clear Methods Example (JScript) | 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: - "JScript" helpviewer_keywords: - "Requery method [ADO], JScript example" - "Clear method [ADO], JScript example" - "Execute method [ADO], JScript example" ms.assetid: 51a87e91-c9d9-4e49-af47-79cce2c4cfe0 author: MightyPen ms.author: genemi --- # Execute, Requery, and Clear Methods Example (JScript) This example demonstrates the **Execute** method when run from both a [Command](../../../ado/reference/ado-api/command-object-ado.md) object and a [Connection](../../../ado/reference/ado-api/connection-object-ado.md) object. It also uses the [Requery](../../../ado/reference/ado-api/requery-method.md) method to retrieve current data in a [Recordset](../../../ado/reference/ado-api/recordset-object-ado.md), and the [Clear](../../../ado/reference/ado-api/clear-method-ado.md) method to clear the contents of the [Errors](../../../ado/reference/ado-api/errors-collection-ado.md) collection. (The **Errors** collection is accessed via the **Connection** object of the [ActiveConnection](../../../ado/reference/ado-api/activeconnection-property-ado.md) property of the [Recordset](../../../ado/reference/ado-api/recordset-object-ado.md).) Name the file **ExecuteJS.asp**. ``` <%@LANGUAGE="JScript"%> <%// use this meta tag instead of adojavas.inc%> <% strLastName = new String(Request.Form("AuthorLName")); if (strLastName.indexOf("undefined") > -1) strLastName = ""; %> Execute, Requery and Clear Methods Example (JScript)

Execute, Requery and Clear Methods Example (JScript)

<% if (strLastName.length > 0) { // command and recordset variables var Connect = "Provider='sqloledb';Data Source=" + Request.ServerVariables("SERVER_NAME") + ";" + "Initial Catalog='pubs';Integrated Security='SSPI';"; var Cnxn = Server.CreateObject("ADODB.Connection"); var cmdAuthor = Server.CreateObject("ADODB.Command"); var rsAuthor = Server.CreateObject("ADODB.Recordset"); var rsAuthor2 = Server.CreateObject("ADODB.Recordset"); var SQLAuthor2, strMessage, strMessage2; var Err, ErrCount; try { // open connection Cnxn.Open(Connect); // command object parameters cmdAuthor.CommandText = "SELECT * FROM Authors WHERE au_lname = ?"; cmdAuthor.Parameters.Append(cmdAuthor.CreateParameter("Last Name", adChar, adParamInput, 20, strLastName)); cmdAuthor.ActiveConnection = Cnxn; // recordset from command.execute rsAuthor = cmdAuthor.Execute(); // recordset from connection.execute SQLAuthor2 = "SELECT * FROM Authors"; rsAuthor2 = Cnxn.Execute(SQLAuthor2); // check for errors ErrCount = Cnxn.errors.count; if(ErrCount !== 0) //write the errors { for(Err = 0; Err = ErrCount; Err++){ Err = Cnxn.errors.item; Response.Write(Err); } // clean out any existing errors Cnxn.Errors.Clear; } // show the data Response.Write("

"); // first recordset Response.Write("Command.Execute results") while (!rsAuthor.EOF) { // build output string by starting a new line strMessage = "

"; strMessage += "
"; // recordset data strMessage += rsAuthor("au_fname") + " "; strMessage += rsAuthor("au_lname") + " "; // end the line strMessage += "

"; // show the results Response.Write(strMessage); // get next record rsAuthor.MoveNext; } Response.Write("

"); // second recordset Response.Write("Connection.Execute results") while (!rsAuthor2.EOF) { // start a new line strMessage2 = "

"; // first and last name are in first column strMessage2 += rsAuthor2("au_fname") + " " strMessage2 += rsAuthor2("au_lname") + " "; // end the line strMessage2 += "

"; // show results Response.Write(strMessage2); // get next record rsAuthor2.MoveNext; } } catch (e) { Response.Write(e.message); } finally { // clean up if (rsAuthor.State == adStateOpen) rsAuthor.Close; if (rsAuthor2.State == adStateOpen) rsAuthor2.Close; if (Cnxn.State == adStateOpen) Cnxn.Close; rsAuthor1 = null; rsAuthor2 = null; Cnxn = null; } } %>

Enter last name of author to find (e.g., Ringer):

``` ## See Also [Clear Method (ADO)](../../../ado/reference/ado-api/clear-method-ado.md) [Command Object (ADO)](../../../ado/reference/ado-api/command-object-ado.md) [Connection Object (ADO)](../../../ado/reference/ado-api/connection-object-ado.md) [Error Object](../../../ado/reference/ado-api/error-object.md) [Execute Method (ADO Command)](../../../ado/reference/ado-api/execute-method-ado-command.md) [Execute Method (ADO Connection)](../../../ado/reference/ado-api/execute-method-ado-connection.md) [Recordset Object (ADO)](../../../ado/reference/ado-api/recordset-object-ado.md) [Requery Method](../../../ado/reference/ado-api/requery-method.md)