
Ado Command in ASP
Using ADO Command in ASP (Classic ASP with VBScript) allows you to execute parameterized SQL queries, which is more secure and flexible than inline SQL strings.
? Basic Steps to Use ADODB.Command
in Classic ASP
Create and open a database connection.
Create an
ADODB.Command
object.Set its properties (SQL, connection, type).
Add parameters if needed.
Execute the command.
? Example: Insert Using ADODB.Command
<%' Step 1: Open ConnectionDim connSet conn = Server.CreateObject("ADODB.Connection")conn.Open "Provider=SQLOLEDB;Data Source=SERVER_NAME;Initial Catalog=DB_NAME;User ID=USERNAME;Password=PASSWORD;"' Step 2: Prepare CommandDim cmdSet cmd = Server.CreateObject("ADODB.Command")Set cmd.ActiveConnection = conncmd.CommandText = "INSERT INTO Users (Name, Email) VALUES (?, ?)"cmd.CommandType = 1 ' adCmdText' Step 3: Add Parameterscmd.Parameters.Append cmd.CreateParameter("Name", 200, 1, 50, "John Doe") ' 200 = adVarChar, 1 = adParamInputcmd.Parameters.Append cmd.CreateParameter("Email", 200, 1, 50, "john@example.com")' Step 4: Executecmd.Execute' Step 5: CleanupSet cmd = Nothingconn.CloseSet conn = Nothing%>
? Notes:
CommandType = 1
meansadCmdText
, used for direct SQL statements.Use
?
placeholders in the SQL and bind parameters in the order they appear.adVarChar = 200
,adParamInput = 1
For
INSERT
,UPDATE
, andDELETE
, usecmd.Execute
directly.
? Example: Stored Procedure with Parameters
<%Set conn = Server.CreateObject("ADODB.Connection")conn.Open "your_connection_string"Set cmd = Server.CreateObject("ADODB.Command")Set cmd.ActiveConnection = conncmd.CommandText = "InsertUser"cmd.CommandType = 4 ' adCmdStoredProccmd.Parameters.Append cmd.CreateParameter("@Name", 200, 1, 50, "John")cmd.Parameters.Append cmd.CreateParameter("@Email", 200, 1, 50, "john@example.com")cmd.ExecuteSet cmd = Nothingconn.CloseSet conn = Nothing%>
Would you like an example with SELECT
and record retrieval using Command
as well?