ADO.CreateADOConnection Method

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

Description

Creates a new IAQAADOConnection object and returns a reference to it. This object holds all information necessary to establish a connection to a database.

Declaration

ADO.CreateADOConnection()

Result An IAQAADOConnection object

Applies To

The method is applied to the following object:

Result Value

An object of the IAQAADOConnection type.

Remarks

The IAQAADOConnection object is an analogue of a Borland VCL TADOConnection object. Methods and properties are identical, with the exception that there are two variants for the VCL's Execute method, which is overloaded:

Method Description
Execute Does not return any value. Use this method if the executed command does not return records.
Execute_ Returns a Microsoft ADO Recordset object. This method is for commands that return sets of records, such as a SELECT query, or any command returning a table.

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

Example

The following example illustrates how you can use the IAQAADOConnection object in your scripts. The TestProc routine 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/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 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 15 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 15 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 15 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 15 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"]();
}

See Also

ADO.CreateCommand

Highlight search results