You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Classic ASP编写参数化查询时遭遇ADODB.Command错误

解决Classic ASP参数化查询的ADODB.Command错误 '800a0bb9'

错误原因

出现ADODB.Command error '800a0bb9'且报错在cmd.CommandType = adCmdText这行,核心原因是Classic ASP默认未内置ADODB常量(比如adCmdText、adInteger、adParamInput等),这些常量本质是对应数字值,直接使用未定义的常量会触发类型不匹配或参数冲突错误。

解决方案

方法1:手动定义所需ADODB常量

在代码开头添加常量定义,对应标准值如下:

' ADODB核心常量定义
Const adCmdText = 1
Const adInteger = 3
Const adParamInput = 1
Const adVarChar = 200

方法2:引用ADODB类型库(不推荐生产环境使用)

在ASP页面顶部添加类型库引用,但该方式可能受服务器配置限制:

<!-- METADATA TYPE="typelib" UUID="00000205-0000-0010-8000-00AA006D2EA4" -->

修正后的完整代码

' 定义ADODB常量
Const adCmdText = 1
Const adInteger = 3
Const adParamInput = 1
Const adVarChar = 200

Dim requestedID
requestedID = Request("ID")

Dim requestedName
requestedName = Request("Name")

' 构造参数化SQL语句
Dim strSQL
strSQL = "SELECT * FROM customer WHERE (ID = ?) AND (Name= ?)"

' 创建Command对象
Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = strSQL
cmd.CommandType = adCmdText

' 创建参数并绑定值
Dim paramID, paramName 
Set paramID = cmd.CreateParameter("@ID", adInteger, adParamInput, , requestedID)
Set paramName = cmd.CreateParameter("@Name", adVarChar, adParamInput, 200, requestedName)

' 将参数添加到Command对象
cmd.Parameters.Append paramID
cmd.Parameters.Append paramName 

' 执行查询
Dim rs
Set rs = cmd.Execute

额外注意事项

  • 建议对requestedID做类型转换(比如requestedID = CInt(Request("ID"))),确保传入的是有效整数,避免参数类型不匹配。
  • adVarChar的长度参数(示例中为200)需与数据库Name字段的实际长度一致,避免截断或参数错误。

内容的提问来源于stack exchange,提问作者Babar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 17:52:46