Description
This function creates a new ADO Command
object and returns a reference to it. This object will allow you to execute queries with parameters from your scripts.
For detailed information on the Command
object, see documentation on Microsoft ADO objects in the MSDN Library.
Declaration
ADO.CreateCommand()
Result | A Command object (Microsoft ADO) |
Applies To
The method is applied to the following object:
Result Value
A Microsoft ADO Command
object.
Remarks
-
You can also create a new ADO
Command
object by its ProgID, ADODB.Command, using theSys.OleObject
routine or thegetActiveXObject
method (JavaScript only):JavaScript
rs = getActiveXObject("ADODB.Command");
JScript
rs = Sys.OleObject("ADODB.Command");
// -- or --
rs = new ActiveXObject("ADODB.Command");Python
rs = Sys.OleObject["ADODB.Command"]
VBScript
Set rs = Sys.OleObject("ADODB.Command")
' -- or --
Set rs = CreateObject("ADODB.Command")DelphiScript
rs := Sys.OleObject['ADODB.Command'];
C++Script, C#Script
rs = Sys["OleObject"]("ADODB.Command");
// -- or --
rs = new ActiveXObject("ADODB.Command"); -
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.
Example
The following code demonstrates the use of the ADO Command
object. The TestProc
routine executes a query with parameters and posts query results 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. TestComplete 32-bit executable is located in the <TestComplete>\Bin folder. |
JavaScript
function Main()
{
var AConnection, RecSet, Cmd, Prm;
// Create a new Connection object
AConnection = ADO.CreateConnection();
// Note that you can also create an ADO 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";
// Activate the connection
AConnection.Open();
// Create a new Command object
Cmd = ADO.CreateCommand();
// To create an ADO command you can also use the following
code:
// Cmd = getActiveXObject("ADODB.Command");
// Specify the connection
Cmd.ActiveConnection = AConnection;
// Specify command type and text
Cmd.CommandType = adCmdText;
Cmd.CommandText = "SELECT * FROM Authors WHERE Authors.[Year Born] < :MyParam";
// Create a new parameter
Prm = Cmd.CreateParameter("MyParam", adInteger, adParamInput);
Cmd.Parameters.Append(Prm);
// Specify the parameter value
Prm.Value = 1940;
// Execute the command
RecSet = Cmd.Execute();
// Process the results
RecSet.MoveFirst();
while (! RecSet.EOF )
{
Log.Message(RecSet.Fields.Item("Author").Value, RecSet.Fields.Item("Year Born").Value);
RecSet.MoveNext();
}
AConnection.Close();
}
JScript
function Main()
{
var AConnection, RecSet, Cmd, Prm;
// Create a new Connection object
AConnection = ADO.CreateConnection();
// Note that you can also create an ADO 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";
// Activate the connection
AConnection.Open();
// Create a new Command object
Cmd = ADO.CreateCommand();
// To create an ADO command you can also use the following
code:
// Cmd = new ActiveXObject("ADODB.Command");
// Specify the connection
Cmd.ActiveConnection = AConnection;
// Specify command type and text
Cmd.CommandType = adCmdText;
Cmd.CommandText = "SELECT * FROM Authors WHERE Authors.[Year Born] < :MyParam";
// Create a new parameter
Prm = Cmd.CreateParameter("MyParam", adInteger, adParamInput);
Cmd.Parameters.Append(Prm);
// Specify the parameter value
Prm.Value = 1940;
// Execute the command
RecSet = Cmd.Execute();
// Process the results
RecSet.MoveFirst();
while (! RecSet.EOF )
{
Log.Message(RecSet.Fields("Author").Value, RecSet.Fields("Year Born").Value);
RecSet.MoveNext();
}
AConnection.Close();
}
Python
def Main():
# Create a new 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"
# Activate the connection
AConnection.Open()
# Create a new Command object
Cmd = ADO.CreateCommand()
# Specify the connection
Cmd.ActiveConnection = AConnection
# Specify command type and text
Cmd.CommandType = adCmdText
Cmd.CommandText = "SELECT * FROM Authors WHERE Authors.[Year Born] < :MyParam"
# Create a new parameter
Prm = Cmd.CreateParameter("MyParam", adInteger, adParamInput)
Cmd.Parameters.Append(Prm)
# Specify the parameter value
Prm.Value = 1940
# Execute the command
RecSet = Cmd.Execute()
# Process the results
RecSet.MoveFirst()
while not RecSet.EOF:
Log.Message(RecSet.Fields.Item["Author"].Value, RecSet.Fields.Item["Year Born"].Value)
RecSet.MoveNext()
AConnection.Close()
VBScript
Sub Main
' Create a new Connection object
Set AConnection = ADO.CreateConnection
' Note that you can also create an ADO 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"
' Activate the connection
AConnection.Open
' Create a new Command object
Set Cmd = ADO.CreateCommand
' To create an ADO command you can also use the following
code:
' Set Cmd = CreateObject("ADODB.Command")
' Specify the connection
Cmd.ActiveConnection = AConnection
' Specify command type and text
Cmd.CommandText = "SELECT * FROM Authors WHERE Authors.[Year Born] < :MyParam"
Cmd.CommandType = adCmdText
' Create a new parameter
Set Prm = Cmd.CreateParameter("MyParam", adInteger, adParamInput)
Cmd.Parameters.Append Prm
' Specify the parameter value
Prm.Value = 1940
' Execute the command
Set RecSet = Cmd.Execute
' Execute the command
RecSet.MoveFirst
While Not RecSet.EOF
Log.Message RecSet("Author").Value, RecSet("Year Born").Value
RecSet.MoveNext
WEnd
AConnection.Close
End Sub
DelphiScript
procedure Main;
var
AConnection, RecSet, Cmd, Prm : OleVariant;
begin
// Create a new Connection object
AConnection := ADO.CreateConnection;
// Note that you can also create an ADO 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';
// Activate the connection
AConnection.Open;
// Create a new Command object
Cmd := ADO.CreateCommand;
// To create an ADO command you can also use the following
code:
// Cmd := Sys.OleObject('ADODB.Command');
// Specify the connection
Cmd.ActiveConnection := AConnection;
// Specify command type and text
Cmd.CommandText := 'SELECT * FROM Authors WHERE Authors.[Year Born] < :MyParam';
Cmd.CommandType := adCmdText;
// Create a new parameter
Prm := Cmd.CreateParameter('MyParam', adInteger, adParamInput);
Cmd.Parameters.Append(Prm);
// Specify the parameter value
Prm.Value := 1940;
// Execute the command
RecSet := Cmd.Execute;
// Process the results
RecSet.MoveFirst;
while not aqConvert.VarToBool(RecSet.EOF) do
begin
Log.Message(RecSet.Fields('Author').Value, RecSet.Fields('Year Born').Value);
RecSet.MoveNext;
end;
AConnection.Close;
end;
C++Script, C#Script
function Main()
{
var AConnection, RecSet, Cmd, Prm;
// Create a new Connection object
AConnection = ADO["CreateConnection"]();
// Note that you can also create an ADO 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";
// Activate the connection
AConnection["Open"]();
// Create a new Command object
Cmd = ADO["CreateCommand"]();
// To create an ADO command you can also use the following
code:
// Cmd = new ActiveXObject("ADODB.Command");
// Specify the connection
Cmd["ActiveConnection"] = AConnection;
// Specify command type and text
Cmd["CommandType"] = 1 /* adCmdText */;
Cmd["CommandText"] = "SELECT * FROM Authors WHERE Authors.[Year Born] < :MyParam";
// Create a new parameter
Prm = Cmd["CreateParameter"]("MyParam", 3 /* adInteger */, 1 /* adParamInput */);
Cmd["Parameters"]["Append"](Prm);
// Specify the parameter value
Prm["Value"] = 1940;
// Execute the command
RecSet = Cmd["Execute"]();
// Process the results
RecSet["MoveFirst"]();
while (!RecSet["EOF"])
{
Log["Message"](RecSet["Fields"]("Author")["Value"], RecSet["Fields"]("Year Born")["Value"]);
RecSet["MoveNext"]();
}
AConnection["Close"]();
}
See Also
ADO.CreateADOCommand Method
ADO.CreateRecordset Method
ADO.CreateConnection Method