ADO.CreateADOCommand Method

Applies to TestComplete 15.70, last modified on December 17, 2024

Description

Creates a new IAQAADOCommand object and returns a reference to it. This object allows scripts to execute queries on ADO databases.

Declaration

ADO.CreateADOCommand()

Result An IAQAADOCommand object

Applies To

The method is applied to the following object:

Result Value

An object of the IAQAADOCommand type.

Remarks

The IAQAADOCommand object is an analogue of a Borland VCL TADOCommand object. Methods and properties are identical, with the exception that there are three variants for the VCL's Executes method, which is overloaded:

Method Description
Execute Does not use any parameters.
Returns an ADO Recordset object.
Execute_ Uses the only parameter that specifies command parameters.
Returns an ADO Recordset object.
Execute__ Uses two parameters: RowsAffected and Parameters.
Returns an ADO Recordset object.

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

Example

The following example illustrates how you can use the CreateADOCommand method in your scripts. The TestProc routine uses the IAQADOCommand object to execute a query with parameters. Query results are then posted to 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 RecSet, Cmd;
  // Create a new object
  Cmd = ADO.CreateADOCommand();
  // Specify the connection string
  Cmd.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=C:\\Users\\Public\\Documents\\TestComplete 15 Samples\\Desktop\\Checkpoints\\XML\\DataGridViewSample\\OrdersDB.mdb";
  // Specify the command text (the SQL expression)
  Cmd.CommandText = "SELECT * FROM orders WHERE orders.[state] = :MyParam";
  // Specify the command type
  Cmd.CommandType = cmdText;
  // Specify the parameter of the query
  Cmd.Parameters.Items(0).Value = "Canada";
  // Execute the command
  RecSet = Cmd.Execute();
  // Process the table records
  Log.AppendFolder("Customers from Canada");
  RecSet.MoveFirst();
  while (! RecSet.EOF)
  {
    Log.Message(RecSet.Fields.Item("name").Value);
    RecSet.MoveNext();
  };
}

Python

def TestProc():
  # Create a new object
  Cmd = ADO.CreateADOCommand()
  # Specify the connection string
  Cmd.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + \
  'Data Source=C:\\Users\\Public\\Documents\\TestComplete 14 Samples\\Desktop\\Checkpoints\\XML\\DataGridViewSample\\OrdersDB.mdb'
  # Specify the command text (the SQL expression)
  Cmd.CommandText = 'SELECT * FROM orders WHERE orders.[state] = :MyParam'
  # Specify the command type
  Cmd.CommandType = cmdText
  # Specify the parameter of the query
  Cmd.Parameters.Items[0].Value = 'Canada'
  # Execute the command
  RecSet = Cmd.Execute()
  # Process the table records
  Log.AppendFolder('Customers from Canada')
  RecSet.MoveFirst()
  while not RecSet.EOF:
    Log.Message(RecSet.Fields.Item['name'].Value)
    RecSet.MoveNext()

VBScript

Sub TestProc
  ' Create a new object
  Set Cmd = ADO.CreateADOCommand
  ' Specify the connection string
  Cmd.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + _
  "Data Source=C:\Users\Public\Documents\TestComplete 15 Samples\Desktop\Checkpoints\XML\DataGridViewSample\OrdersDB.mdb"
  ' Specify the command text (the SQL expression)
  Cmd.CommandText = "SELECT * FROM orders WHERE orders.[state] = :MyParam"
  ' Specify the command type
  Cmd.CommandType = cmdText
  ' Specify the parameter of the query
  Cmd.Parameters.Items(0).Value = "Canada"
  ' Execute the command
  Set RecSet = Cmd.Execute
  ' Process the table records
  Log.AppendFolder("Customers from Canada")
  RecSet.MoveFirst
  While Not RecSet.EOF
    Log.Message RecSet.Fields("name").Value
    RecSet.MoveNext
  Wend
End Sub

DelphiScript

procedure TestProc;
var
  RecSet, Cmd: OleVariant;
begin
  // Create a new object
  Cmd := ADO.CreateADOCommand;
  // Specify the connection string
  Cmd.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;'+
  'Data Source=C:\Users\Public\Documents\TestComplete 15 Samples\Desktop\Checkpoints\XML\DataGridViewSample\OrdersDB.mdb';
  // Specify the command text (the SQL expression)
  Cmd.CommandText := 'SELECT * FROM orders WHERE orders.[state] = :MyParam';
  // Specify the command type
  Cmd.CommandType := cmdText;
  // Specify the parameter of the query
  Cmd.Parameters.Items(0).Value := 'Canada';
  // Execute the command
  RecSet := Cmd.Execute;
  // Process the table records
  Log.AppendFolder('Customers from Canada');
  RecSet.MoveFirst;
  while not aqConvert.VarToBool(RecSet.EOF) do
  begin
    Log.Message(RecSet.Fields('name').Value);
    RecSet.MoveNext;
  end;
end;

C++Script, C#Script

function TestProc()
{
  var RecSet, Cmd;
  // Create a new object
  Cmd = ADO["CreateADOCommand"]();
  // Specify the connection string
  Cmd["ConnectionString"] = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=C:\\Users\\Public\\Documents\\TestComplete 15 Samples\\Desktop\\Checkpoints\\XML\\DataGridViewSample\\OrdersDB.mdb";
  // Specify the command text (the SQL expression)
  Cmd["CommandText"] = "SELECT * FROM orders WHERE orders.[state] = :MyParam";
  // Specify the command type
  Cmd["CommandType"] = cmdText;
  // Specify the parameter of the query
  Cmd["Parameters"]["Items"](0)["Value"] = "Canada";
  // Execute the command
  RecSet = Cmd["Execute"]();
  // Process the table records
  Log["AppendFolder"]("Customers from Canada");
  RecSet["MoveFirst"]();
  while (!RecSet["EOF"])
  {
    Log["Message"](RecSet["Fields"]("name")["Value"]);
    RecSet["MoveNext"]();
  };
}

See Also

ADO.CreateCommand

Highlight search results