ADO.CreateADOQuery Method

Applies to TestComplete 15.47, last modified on January 20, 2023

Description

Creates a new IAQAADOQuery object and returns a reference to it.

Declaration

ADO.CreateADOQuery()

Result An IAQAADOQuery object

Applies To

The method is applied to the following object:

Result Value

An object of the IAQAADOQuery type.

Remarks

The IAQAADOQuery object is an analogue of a Borland VCL TADOQuery object. Methods and properties are identical.

For detailed information on the underlying TADOQuery object, see VCL documentation on ADODB classes.

Once the IAQAADOQuery object is created, you can use it to send queries to databases. To do this:

  • Establish a connection to the database (set the Connection or ConnectionString property).
  • Specify the SQL expression (set the SQL property).
  • If necessary, specify the query parameters (set the Parameters property).
  • Run the query using the Open or ExecSQL methods.

Example

The following code illustrates how you can use the IAQAADOQuery object to retrieve data from the databases. The TestProc routine executes the query and inserts results into the test log.

Notes:

  • This example uses the OrdersDB.mdb file that is part of the additional sample package. To use it, download this package from support.smartbear.com/testcomplete/downloads/samples and install it. After the installation is over, you can find the database in the <TestComplete 15 Samples>\Desktop\Checkpoints\XML\DataGridViewSample folder.

  • The syntax of the command’s query depends on the SQL provider you use. For instance, if you work through the Microsoft.Jet.OLEDB.4.0 provider, you can specify parameters by their names. Some other providers may use question marks (?) as parameter placeholders (the first question mark corresponds to the first parameter, the second - to the second and so on). Some providers may support both techniques, or use their own syntax. For more information on the command syntax, see the database provider's documentation.

  • Using the Microsoft.Jet.OLEDB.4.0 provider requires that you run your script in the 32-bit version of TestComplete.

JavaScript, JScript

function TestProc()
{
  var Qry;
  // Create a query
  Qry = ADO.CreateADOQuery();
  // Specify the connection string
  Qry.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=C:\\Users\\Public\\Documents\\TestComplete 15 Samples\\Desktop\\Checkpoints\\XML\\DataGridViewSample\\OrdersDB.mdb";
  // Specify the SQL expression
  Qry.SQL = "SELECT * FROM orders WHERE orders.[state] = :Param_State";
  // Specify the parameter value
  Qry.Parameters.ParamByName("Param_State").Value = "Canada";
  // Execute the query
  Qry.Open();
  // Process results and insert data into the test log
  Log.AppendFolder("Customers from Canada");
  Qry.First();
  while (! Qry.EOF)
  {
    Log.Message(Qry.FieldByName("name").Value);
    Qry.Next();
  };
  // Close the query
  Qry.Close();
}

Python

def TestProc():
  # Create a query
  Qry = ADO.CreateADOQuery()
  # Specify the connection string
  Qry.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + \
  "Data Source=C:\\Users\\Public\\Documents\\TestComplete 14 Samples\\Desktop\\Checkpoints\\XML\\DataGridViewSample\\OrdersDB.mdb"
  # Specify the SQL expression
  Qry.SQL = "SELECT * FROM orders WHERE orders.[state] = :Param_State"
  # Specify the parameter value
  Qry.Parameters.ParamByName("Param_State").Value = "Canada"
  # Execute the query
  Qry.Open()
  # Process results and insert data into the test log
  Log.AppendFolder("Customers from Canada")
  Qry.First()
  while not Qry.EOF:
    Log.Message(Qry.FieldByName("name").Value)
    Qry.Next()
  # Close the query
  Qry.Close()

VBScript

Sub TestProc
  ' Create a query
  Set Qry = ADO.CreateADOQuery
  ' Specify the connection string
  Qry.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + _
  "Data Source=C:\Users\Public\Documents\TestComplete 15 Samples\Desktop\Checkpoints\XML\DataGridViewSample\OrdersDB.mdb"
  ' Specify the SQL expression
  Qry.SQL = "SELECT * FROM orders WHERE orders.[state] = :Param_State"
  ' Specify the parameter value
  Qry.Parameters.ParamByName("Param_State").Value = "Canada"
  ' Execute the query
  Qry.Open
  ' Process results and insert data into the test log
  Log.AppendFolder("Customers from Canada")
  Qry.First
  While Not Qry.EOF
    Log.Message Qry.FieldByName("name").Value
    Qry.Next
  Wend
  ' Close the query
  Qry.Close
End Sub

DelphiScript

procedure TestProc;
var
  Qry : OleVariant;
begin
  // Create a query
  Qry := ADO.CreateADOQuery();
  // Specify the connection string
  Qry.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
  'Data Source=C:\Users\Public\Documents\TestComplete 15 Samples\Desktop\Checkpoints\XML\DataGridViewSample\OrdersDB.mdb';
  // Specify the SQL expression
  Qry.SQL := 'SELECT * FROM orders WHERE orders.[state] = :Param_State';
  // Specify the parameter value
  Qry.Parameters.ParamByName['Param_State'].Value := 'Canada';
  // Execute the query
  Qry.Open;
  // Process results and insert data into the test log
  Log.AppendFolder('Customers from Canada');
  Qry.First;
  while not aqConvert.VarToBool(Qry.EOF) do
  begin
    Log.Message(Qry.FieldByName('name').Value);
    Qry.Next;
  end;
  // Close the query
  Qry.Close
end;

C++Script, C#Script

function TestProc()
{
  var Qry;
  // Create a query
  Qry = ADO["CreateADOQuery"]();
  // Specify the connection string
  Qry["ConnectionString"] = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=C:\\Users\\Public\\Documents\\TestComplete 15 Samples\\Desktop\\Checkpoints\\XML\\DataGridViewSample\\OrdersDB.mdb";
  // Specify the SQL expression
  Qry["SQL"] = "SELECT * FROM orders WHERE orders.[state] = :Param_State";
  // Specify the parameter value
  Qry["Parameters"]["ParamByName"]("Param_State")["Value"] = 'Canada';
  // Execute the query
  Qry["Open"]();
  // Process results and insert data into the test log
  Log["AppendFolder"]("Customers from Canada");
  Qry["First"]();
  while (!Qry["EOF"])
  {
    Log["Message"](Qry["FieldByName"]("name")["Value"]);
    Qry["Next"]();
  }
  // Close the query
  Qry["Close"]();
}

See Also

ADO.CreateADOTable
ADO.CreateADODataset
ADO.CreateADOStoredProc

Highlight search results