ADO.CreateConnection Method

Applies to TestComplete 15.69, last modified on November 13, 2024

Description

This function creates a new ADO Connection object and returns a reference to it. This object will allow you to connect to a database, to obtain database tables and to execute simple queries.

For detailed information on the Connectionobject, see documentation on Microsoft ADO objects in the MSDN Library.

Declaration

ADO.CreateConnection()

Result A Connection object (Microsoft ADO)

Applies To

The method is applied to the following object:

Result Value

A Microsoft ADO Connection object.

Remarks

Note that you can also create a new ADO Connection object by its ProgID, ADODB.Connection, using the Sys.OleObject routine or the getActiveXObject method (JavaScript only):

JavaScript

rs = getActiveXObject("ADODB.Connection");

JScript

rs = Sys.OleObject("ADODB.Connection");
// -- or --
rs = new ActiveXObject("ADODB.Connection");

Python

rs = Sys.OleObject["ADODB.Connection"]

VBScript

Set rs = Sys.OleObject("ADODB.Connection")
' -- or --
Set rs = CreateObject("ADODB.Connection")

DelphiScript

rs := Sys.OleObject['ADODB.Connection'];

C++Script, C#Script

rs = Sys["OleObject"]("ADODB.Connection");
// -- or --
rs = new ActiveXObject("ADODB.Connection");

Example

The following example demonstrates how you can use the ADO Connection object. The TestProc procedure connects to the database, executes the query, then 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.CreateConnection();
  // 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";
  // Open the connection
  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();
   }
   // Close the connection
   AConnection.Close();
 }

Python

def TestProc():
  # Create a Connection object
  AConnection = ADO.CreateConnection()
  # 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"
  # Open the connection
  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()
  # Close the connection
  AConnection.Close()

VBScript

Sub TestProc
  ' Create a Connection object
  Set AConnection = ADO.CreateConnection
  ' 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"
  ' Open the database
  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
  ' Close the connection
  AConnection.Close
End Sub

DelphiScript

procedure TestProc;
var
  AConnection, RecSet : OleVariant;
begin
  // Create a new Connection object
  AConnection := ADO.CreateConnection;
  // 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';
  // Open the connection
  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;
  // Close the connection
  AConnection.Close;
end;

C++Script, C#Script

function TestProc()
 {
  var AConnection, RecSet;
  // Create a Connection object
  AConnection = ADO["CreateConnection"]();
  // 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";
  // Open the connection
  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"]();
  };
  // Close the connection
  AConnection["Close"]();
}

See Also

CreateADOConnection Method
CreateRecordset Method
CreateCommand Method

Highlight search results