ADO.CreateConnection Method

Applies to TestComplete 12.60, last modified on September 17, 2018

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 uses the Biblio.mdb file shipped with Microsoft Visual Basic. The TestProc procedure connects to the database, processes the first ten records of the Authors table and posts data into the test log.

Note:

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

JavaScript

function TestProc()
{
  var AConnection, RecSet, i;
  // Create a Connection object
  AConnection = ADO.CreateConnection();
  // Note that you can also create a connection using the following code:
  // AConnection = getActiveXObject("ADODB.Connection");


  // Specify the connection string
  AConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=C:\\Microsoft Visual Studio\\VB98\\biblio.mdb";
  // Open the connection
  AConnection.Open();
  // Return the Authors table
  RecSet = AConnection.Execute("Authors", i, adCmdTable);
  // Process the first ten records and post data to the test log
  RecSet.MoveFirst();
  for (let i = 1; i <= 10; i++)
  {
    Log.Message(RecSet.Fields.Item("Author").Value);
    RecSet.MoveNext()
  }
  // Close the connection
  AConnection.Close();
}

JScript

function TestProc()
{
  var AConnection, RecSet, i;
  // Create a Connection object
  AConnection = ADO.CreateConnection();
  // Note that you can also create a connection using the following code:
  // AConnection = new ActiveXObject("ADODB.Connection");


  // Specify the connection string
  AConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=C:\\Microsoft Visual Studio\\VB98\\biblio.mdb";
  // Open the connection
  AConnection.Open();
  // Return the Authors table
  RecSet = AConnection.Execute("Authors", i, adCmdTable);
  // Process the first ten records and post data to the test log
  RecSet.MoveFirst();
  for (i = 1; i <= 10; i++)
  {
    Log.Message(RecSet.Fields("Author").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:\\Microsoft Visual Studio\\VB98\\biblio.mdb"
  # Open the connection
  AConnection.Open()
  # Return the Authors table
  RecSet = AConnection.Execute("Authors", adCmdTable)
  # Process the first ten records and post data to the test log
  RecSet.MoveFirst
  for i in range(1, 10):
    Log.Message(RecSet.Fields.Item["Author"].Value)
    RecSet.MoveNext()
  # Close the connection
  AConnection.Close()

VBScript

Sub TestProc
  ' Create a Connection object
  Set AConnection = ADO.CreateConnection
  ' Note that you can also create a connection using the following code:
  ' Set AConnection = CreateObject("ADODB.Connection")


  ' Specify the connection string
  AConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  "Data Source=C:\Microsoft Visual Studio\VB98\biblio.mdb"
  ' Open the database
  AConnection.Open
  ' Return the Authors table
  Set RecSet = AConnection.Execute("Authors", i, adCmdTable)
  ' Iterate through the records and
  ' post messages to the test log
  RecSet.MoveFirst
  For i = 1 To 10
    Log.Message RecSet.Fields("Author").Value
    RecSet.MoveNext
  Next
  ' Close the connection
  AConnection.Close
End Sub

DelphiScript

procedure TestProc;
var
  AConnection, RecSet : OleVariant;
  i : integer;
begin
  // Create a new Connection object
  AConnection := ADO.CreateADOConnection;
  // Note that you can also create a connection using the following code:
  // AConnection := Sys.OleObject('ADODB.Connection');


  // Specify the connection string
  AConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
  'Data Source=C:\Microsoft Visual Studio\VB98\biblio.mdb';
  // Suppress the login dialog box
  AConnection.LoginPrompt := False;
  // Open the connection
  AConnection.Open;
  // Return the Authors table
  RecSet := AConnection.Execute_('Authors', i, adCmdTable);
  // Process the first ten records
  RecSet.MoveFirst;
  for i := 1 to 10 do
  begin
    Log.Message(RecSet.Fields('Author').Value);
    RecSet.MoveNext;
  end;
  // Close the connection
  AConnection.Close;
end;

C++Script, C#Script

function TestProc()
{
  var AConnection, RecSet, i;
  // Create a Connection object
  AConnection = ADO["CreateConnection"]();
  // Note that you can also create a connection using the following code:
  // AConnection = new ActiveXObject("ADODB.Connection");


  // Specify the connection string
  AConnection["ConnectionString"] = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=C:\\Microsoft Visual Studio\\VB98\\biblio.mdb";
  // Open the connection
  AConnection["Open"]();
  // Return the Authors table
  RecSet = AConnection["Execute"]("Authors", i, adCmdTable);
  // Process the first ten records and post data to the test log
  RecSet["MoveFirst"]();
  for (i = 1; i <= 10; i++)
  {
    Log["Message"](RecSet["Fields"]("Author")["Value"]);
    RecSet["MoveNext"]()
  }
  // Close the connection
  AConnection["Close"]();
}

See Also

ADO.CreateADOConnection Method
ADO.CreateRecordset Method
ADO.CreateCommand Method

Highlight search results