ADO - Connection.Execute vs Command.Execute
When to use Connection.Execute vs Command.Execute (deep dive)
Short answer up-front:
-
Use
Connection.Executefor quick, one-off SQL statements (simple SELECT/UPDATE/INSERT/DELETE) where you don’t need parameters, output values, or repeated execution. -
Use
Command.Executewhen you need parameterization, stored-procedure support, prepared statements, output/return parameters, command-level properties (timeout, prepared), or repeated execution for better safety and performance.
Below is a detailed comparison, examples, and best-practice checklist.
What each does
Connection.Execute
-
Method on the
Connectionobject:Connection.Execute(CommandText, RecordsAffected, Options) -
Runs a SQL string (or text that can be interpreted as SQL).
-
Fast and succinct for ad-hoc SQL.
-
No built-in parameters collection (you must build the SQL string or use positional
?placeholders with provider-specific behavior).
Command.Execute
-
Method on the
Commandobject:Command.Execute(RecordsAffected, Parameters, Options) -
The
Commandobject exposesParameters,CommandType,Prepared,CommandTimeout, and other properties. -
Best for stored procedures, parameterized queries, prepared/repeated execution, output/return values.
Feature-by-feature comparison
-
Parameters
-
Connection.Execute: none (you must inline values or build SQL with?in some providers — generally awkward and error-prone). -
Command.Execute: fullParameterscollection (CreateParameter,Append) — safe, prevents SQL injection.
-
-
Stored procedures & output params
-
Connection.Execute: possible by sendingEXEC proc ...as text, but handling output params is clumsy or impossible directly. -
Command.Execute: intended foradCmdStoredProc+ parameters (input/output/return value).
-
-
Prepared statements / repeated execution
-
Connection.Execute: noPreparedsupport. -
Command.Execute:cmd.Prepared = Trueprepares the statement once — faster for many repeated executions.
-
-
CommandType clarity
-
Connection.Execute: Options parameter can indicate type but usually you pass SQL text. -
Command.Execute:cmd.CommandType(adCmdText, adCmdStoredProc, adCmdTable, etc.) — explicit and reliable.
-
-
CommandTimeout
-
ConnectionandCommandboth have timeout, butCommandexposes it per-command (handy for long-running stored procs).
-
-
Transactions
-
Both work inside
conn.BeginTrans/CommitTrans/RollbackTrans. (Use whichever you prefer;Commandis often used inside transactions with stored procedures.)
-
-
Asynchronous execution
-
Both can support async options (provider-dependent). Use with care; more advanced.
-
-
Multiple resultsets
-
Both return a
Recordsetif a resultset is returned. Users.NextRecordsetto iterate additional sets. Behavior can vary by provider.
-
-
Performance
-
For single simple statements:
Connection.Executeis slightly simpler and minimal overhead. -
For repeated/executed-often queries:
CommandwithPrepared = Trueis significantly better.
-
-
Portability & provider quirks
-
Parameter placeholder behavior (
?vs named) andParameters.Refreshbehavior depend on the OLE DB provider.Commandis generally more portable when using properCommandType.
-
Examples
1) Simple SELECT with Connection.Execute
Dim conn, rs
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=SQLOLEDB;Data Source=MyServer;Initial Catalog=MyDB;User Id=sa;Password=pass;"
Set rs = conn.Execute("SELECT FirstName, LastName FROM Employees WHERE Department='Sales'")
Do Until rs.EOF
Response.Write rs("FirstName") & " " & rs("LastName") & "<br>"
rs.MoveNext
Loop
rs.Close
conn.Close
2) UPDATE with rows affected using Connection.Execute
Dim conn, rows
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connString
conn.Execute "UPDATE Employees SET Salary = Salary * 1.05 WHERE Department = 'Sales'", rows
Response.Write rows & " rows updated."
conn.Close
3) Stored procedure with input & output parameters using Command.Execute
Const adCmdStoredProc = 4
Const adInteger = 3
Const adVarChar = 200
Const adParamInput = 1
Const adParamOutput = 2
Dim conn, cmd, rs
Set conn = Server.CreateObject("ADODB.Connection")
Set cmd = Server.CreateObject("ADODB.Command")
conn.Open connString
Set cmd.ActiveConnection = conn
cmd.CommandText = "usp_GetTotalEmployees"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Append cmd.CreateParameter("@Dept", adVarChar, adParamInput, 50, "Sales")
cmd.Parameters.Append cmd.CreateParameter("@Total", adInteger, adParamOutput)
Set rs = cmd.Execute() ' if stored proc returns rows; otherwise rs may be Nothing
' Read output param
Response.Write "Total employees in Sales: " & cmd.Parameters("@Total").Value
If Not rs Is Nothing Then
rs.Close
End If
conn.Close
4) Prepared command for repeated execution (performance)
Const adCmdText = 1
Const adInteger = 3
Const adParamInput = 1
Dim conn, cmd, i
Set conn = Server.CreateObject("ADODB.Connection")
Set cmd = Server.CreateObject("ADODB.Command")
conn.Open connString
Set cmd.ActiveConnection = conn
cmd.CommandText = "INSERT INTO AuditLog(UserID, ActionDate) VALUES (?, ?)"
cmd.CommandType = adCmdText
' Create parameters (positional `?` placeholders) — provider rules apply
cmd.Parameters.Append cmd.CreateParameter("UserID", adInteger, adParamInput)
cmd.Parameters.Append cmd.CreateParameter("ActionDate", adDBTimeStamp, adParamInput)
cmd.Prepared = True ' prepare once
For i = 1 To 1000
cmd.Parameters("UserID").Value = i
cmd.Parameters("ActionDate").Value = Now()
cmd.Execute
Next
conn.Close
Note: The exact parameter naming/ordering behavior when using ? depends on the provider; named parameters with adCmdText may or may not be supported.
Error handling & diagnostics
-
Use
On Error Resume Nextthen checkIf Err.Number <> 0and inspectconn.Errors(Errors collection) after a failed command. -
CommandandConnectionerrors show up inErrorscollection; check.Description,.Number,.Source.
Common pitfalls & advice
-
SQL injection — never concatenate user input into SQL. Use
Command+Parameters. -
Provider differences — parameter naming and placeholder support vary. Test with your provider (SQLOLEDB vs. SQLNCLI vs others).
-
Parameters.Refresh — populates parameters from a stored proc automatically, but is slow and provider-dependent; prefer to create parameters explicitly.
-
Remember to close recordsets and connections to avoid leaks (and rely on connection pooling for efficiency).
-
For stored procedures with output params, use
Command— handling outputs withConnection.Executeis awkward or impossible. -
Use
cmd.Prepared = Truewhen executing the same statement many times to reduce server parse overhead. -
When you don’t need parameters or reuse:
Connection.Executekeeps code simple and readable.
Decision checklist (quick)
-
Do I need to pass parameters (safe, typed)? → Use Command
-
Do I need output/return parameters? → Use Command
-
Will I execute the same statement many times? → Use Command (Prepared)
-
Is this a one-off simple SELECT/UPDATE with no user input? → Connection.Execute is fine
-
Is this a stored procedure that returns values? → Use Command
-
Is performance for repeated operations important? → Use Command.Prepared
-
Convert any of the examples into your exact environment (SQL Server version, provider, or Classic ASP page), or
-
Show a full pattern with proper
On Errorhandling andconn.Errorsinspection, or -
Produce a tiny cheat-sheet you can copy into your codebase.