TestComplete samples (both built-in and additional) are located in the <Users>\Public\Public Documents\TestComplete 15 Samples folder.
Some file managers display the Public Documents folder as Documents.
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:
-
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.
-
Using the Microsoft.Jet.OLEDB.4.0 provider requires that you run your script in the 32-bit version of TestComplete.
TestComplete 32-bit executable is located in the <TestComplete>\Bin folder.
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 15 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 15 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 15 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 15 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"]();
}