Working With ADO Recordset Object

Applies to TestComplete 14.0, last modified on January 23, 2019

The following example demonstrates how you can use the ADO Recordset object in your scripts. The TestProc procedure connects to the database, creates the recordset, and posts its data to the test log.

Notes:

JavaScript, JScript

function TestProc()
{
  // Create a new Connection object
  var Conn = ADO.CreateConnection();
  Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=C:\\Users\\Public\\Documents\\TestComplete 14 Samples\\Desktop\\Checkpoints\\XML\\DataGridViewSample\\OrdersDB.mdb";
  Conn.Open();
  // Create a new Recordset object
  var Rs = ADO.CreateRecordset();
  Rs.Open("orders", Conn, adOpenStatic, adLockReadOnly, adCmdTable);
  // Read data from the recordset and post them to the test log
  Log.AppendFolder("Customer names");
  Rs.MoveFirst();
  while(! Rs.EOF)
  {
    Log.Message(Rs.Fields.Item("name").Value);
    Rs.MoveNext();
  }
  // Close the recordset and connection
  Rs.Close();
  Conn.Close();
}

Python

def TestProc(): 
  # Create a new Connection object
  Conn = ADO.CreateConnection()
  Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + \
  "Data Source=C:\\Users\\Public\\Documents\\TestComplete 14 Samples\\Desktop\\Checkpoints\\XML\\DataGridViewSample\\OrdersDB.mdb"
  Conn.Open()
  # Create a new Recordset object
  Rs = ADO.CreateRecordset()
  Rs.Open("orders", Conn, adOpenStatic, adLockReadOnly, adCmdTable)
  # Read data from the recordset and post them to the test log
  Log.AppendFolder("Customer names")
  Rs.MoveFirst()
  while not Rs.EOF:
    Log.Message(Rs.Fields.Item["name"].Value)
    Rs.MoveNext()    
  # Close the recordset and connection
  Rs.Close()
  Conn.Close()

VBScript

Sub TestProc
  Dim Conn, Rs
  ' Create a new Connection object
  Set Conn = ADO.CreateConnection
  Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + _
  "Data Source=C:\Users\Public\Documents\TestComplete 14 Samples\Desktop\Checkpoints\XML\DataGridViewSample\OrdersDB.mdb"
  Conn.Open
  ' Create a new Recordset object
  Set Rs = ADO.CreateRecordset
  Rs.Open "orders", Conn, adOpenStatic, adLockReadOnly, adCmdTable
  ' Read data from the recordset and post them to the test log
  Log.AppendFolder "Customer names"
  Rs.MoveFirst
  While Not Rs.EOF
    Log.Message Rs.Fields.Item("name").Value
    Rs.MoveNext
  WEnd
  ' Close the recordset and connection
  Rs.Close
  Conn.Close
End Sub

DelphiScript

procedure TestProc;
var
  Conn, Rs : OleVariant;
begin
   // Create a new Connection object
  Conn := ADO.CreateConnection;
  Conn.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;' +
  'Data Source=C:\Users\Public\Documents\TestComplete 14 Samples\Desktop\Checkpoints\XML\DataGridViewSample\OrdersDB.mdb';
  Conn.Open;
  // Create a new Recordset object
  Rs := ADO.CreateRecordset;
  Rs.Open('orders', Conn, adOpenStatic, adLockReadOnly, adCmdTable);
  // Read data from the recordset and post them to the test log
  Log.AppendFolder('Customer names');
  Rs.MoveFirst;
  while not Rs.EOF do
  begin
    Log.Message(Rs.Fields.Item('name').Value);
    Rs.MoveNext;
  end;
  // Close the recordset and connection
  Rs.Close;
  Conn.Close;
end;

C++Script, C#Script

function TestProc()
{
  // Create a new Connection object
  var Conn = ADO["CreateConnection"]();
  Conn["ConnectionString"] = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Data Source=C:\\Users\\Public\\Documents\\TestComplete 14 Samples\\Desktop\\Checkpoints\\XML\\DataGridViewSample\\OrdersDB.mdb";
  Conn["Open"]();
  // Create a new Recordset object
  var Rs = ADO["CreateRecordset"]();
  // Open the recordset and get the Orders table from the OrdersDB database
  Rs["Open"]("orders", Conn, adOpenStatic, adLockReadOnly,adCmdTable);
  // Read data from the recordset and post them to the test log
  Log["AppendFolder"]("Customer names");
  Rs["MoveFirst"]();
  while(! Rs["EOF"])
  {
    Log["Message"](Rs["Fields"]["Item"]("name")["Value"]);
    Rs["MoveNext"]();
  }
  // Close the recordset and connection
  Rs["Close"]();
  Conn["Close"]();
}

Highlight search results