Applies to TestComplete 14.30, last modified on November 21, 2019

The following example illustrates how you can use the IAQAADOConnection object, based on the TADOConnection class. The TestProc procedure connects to the database, 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/downloads/testcomplete/samples/ and install it. After the installation is over, you can find the database in the <TestComplete 14 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 AConnection, RecSet;
   // Create a Connection object
   AConnection = ADO.CreateADOConnection();
   // Specify the connection string
   AConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"+
   "Data Source=C:\\Users\\Public\\Documents\\TestComplete 14 Samples\\Desktop\\Checkpoints\\XML\\DataGridViewSample\\OrdersDB.mdb";
   // Suppress the login dialog box
   AConnection.LoginPrompt = false;
   AConnection.Open();
   // Execute a simple query
   RecSet = AConnection.Execute_("SELECT * FROM orders WHERE [quant] > 1");
   // Iterate through query results and insert data into the test log
   Log.AppendFolder("Customers who bought more than one product");
   RecSet.MoveFirst();
   while(! RecSet.EOF)
   {
     Log.Message(RecSet.Fields.Item("name").Value);
     RecSet.MoveNext();
   }
   AConnection.Close();
 }

Python

def TestProc():
  # Create a Connection object
  AConnection = ADO.CreateADOConnection()
  # Specify the connection string
  AConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + \
  "Data Source=C:\\Users\\Public\\Documents\\TestComplete 14 Samples\\Desktop\\Checkpoints\\XML\\DataGridViewSample\\OrdersDB.mdb";
  # Suppress the login dialog box
  AConnection.LoginPrompt = False
  AConnection.Open()
  # Execute a simple query
  RecSet = AConnection.Execute_("SELECT * FROM orders WHERE [quant] > 1")
  # Iterate through query results and insert data into the test log
  Log.AppendFolder("Customers who bought more than one product")
  RecSet.MoveFirst()
  while not RecSet.EOF:
    Log.Message(RecSet.Fields.Item["name"]);
    RecSet.MoveNext()
  AConnection.Close()

VBScript

Sub TestProc
  ' Create a Connection object
  Set AConnection = ADO.CreateADOConnection
  ' Specify the connection string
  AConnection.ConnectionString = _
  "Provider=Microsoft.Jet.OLEDB.4.0;"+ _
  "Data Source=C:\Users\Public\Documents\TestComplete 14 Samples\Desktop\Checkpoints\XML\DataGridViewSample\OrdersDB.mdb"
  ' Suppress the login dialog box
  AConnection.LoginPrompt = False
  AConnection.Open
  ' Execute a simple query
  Set RecSet = AConnection.Execute_("SELECT * FROM orders WHERE [quant] > 1")
  ' Iterate through query results and insert data into the test log
  Log.AppendFolder("Customers who bought more than one product")
  RecSet.MoveFirst
  While Not RecSet.EOF
    Log.Message RecSet.Fields("name").Value
    RecSet.MoveNext
  Wend
  AConnection.Close
End Sub

DelphiScript

procedure TestProc;
var
  AConnection, RecSet : OleVariant;
begin
  // Create a Connection object
  AConnection := ADO.CreateADOConnection;
  // Specify the connection string
  AConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
  'Data Source=C:\Users\Public\Documents\TestComplete 14 Samples\Desktop\Checkpoints\XML\DataGridViewSample\OrdersDB.mdb';
  // Suppress the login dialog box
  AConnection.LoginPrompt := False;
  AConnection.Open;
  // Execute a simple query
  RecSet := AConnection.Execute_('SELECT * FROM orders WHERE [quant] > 1');
  // Iterate through query results and insert data into the test log
  Log.AppendFolder('Customers who bought more than one product');
  RecSet.MoveFirst;
  while not aqConvert.VarToBool(RecSet.EOF) do
  begin
    Log.Message(RecSet.Fields('name').Value);
    RecSet.MoveNext;
  end;
  AConnection.Close;
end;

C++Script, C#Script

function TestProc()
{
  var AConnection, RecSet;
  // Create a Connection object
  AConnection = ADO["CreateADOConnection"]();
  // Specify the connection string
  AConnection["ConnectionString"] = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=C:\\Users\\Public\\Documents\\TestComplete 14 Samples\\Desktop\\Checkpoints\\XML\\DataGridViewSample\\OrdersDB.mdb";
  // Suppress the login dialog box
  AConnection["LoginPrompt"] = 0;
  AConnection["Open"]();
  // Execute a simple query
  RecSet = AConnection["Execute_"]("SELECT * FROM orders WHERE [quant] > 1");
  // Iterate through query results and insert data into the test log
  Log["AppendFolder"]("Customers who bought more than one product");
  RecSet["MoveFirst"]();
  while (!RecSet["EOF"])
  {
    Log["Message"](RecSet["Fields"]("name")["Value"]);
    RecSet["MoveNext"]();
  };
  AConnection["Close"]();
}

Highlight search results