在不使用存储过程的情况下将TEXT参数添加到ADODB.Command

时间:2011-12-13 13:31:57

标签: sql-server asp-classic parameters adodb

我需要在包含INSERT列的SQL Server 2008表上执行TEXT,并且ADODB.Command列无法使用TEXT参数

该表定义为:

CREATE TABLE dbo.Test (
    TestId INT NOT NULL IDENTITY(1, 1)
    , CreationDate DATETIME NOT NULL
    , Value TEXT NOT NULL
    , CONSTRAINT pkTest PRIMARY KEY (TestId)
)

测试页面如下:

<!-- METADATA TYPE="typelib" NAME="Microsoft ActiveX Data Objects 2.8 Library" UUID="{2A75196C-D9EB-4129-B803-931327F72D5C}" VERSION="2.8" -->
<%@ CODEPAGE = 65001 LCID = 1040 %>
<%
    Option Explicit
    Response.Clear
    Response.CharSet = "UTF-8"
    Response.CodePage = 65001
    Response.ContentType = "text/plain"
    Dim i : i = 0
    Dim value : value = ""
    For i = 1 To 10000
        If i Mod 3 = 0 Then
            value = value & "a "
        Else
            value = value & "a"
        End If
    Next
    Dim connection : Set connection = Server.CreateObject("ADODB.Connection")
    connection.CommandTimeout = 5
    connection.ConnectionString = "Server=XXX; Database=XXX; Uid=XXX; Pwd=XXX;"
    connection.ConnectionTimeout = 5
    connection.IsolationLevel = adXactReadUncommitted
    connection.Mode = adModeRead
    connection.Provider = "SQLOLEDB"
    connection.Open
    Dim command : Set command = Server.CreateObject("ADODB.Command")
    command.ActiveConnection = connection
    command.CommandText = "insert into dbo.Test (CreationDate, Value) values (getdate(), ?)"
    command.CommandTimeout = 5
    command.CommandType = adCmdText
    command.Parameters.Append command.CreateParameter("Value", adVarChar, adParamInput, 0, value)
    command.Prepared = True
    command.Execute
    Set command = Nothing
    connection.Close
    Set connection = Nothing
    Response.End
%>

当我执行它时,我收到此错误:

ADODB.Command error '800a0d5d'
Application uses a value of the wrong type for the current operation.
/test/default.asp, line 32

第32行

command.Parameters.Append command.CreateParameter("Value", adVarChar, adParamInput, 0, value)

我尝试更改Size参数,但没有用。

环顾网络我找到了名为How to call SQL Server stored procedures from ASP的Microsoft知识库文章,其“方法1”示例很有意思,因为它没有声明参数,而是从服务器读取它们:

cmd.CommandText = "sp_test"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Refresh
cmd.Parameters(1) = 11

所以我创建了一个存储过程来执行INSERT

CREATE PROCEDURE dbo.TestInsertion (@CreationDate DATETIME, @Value TEXT) AS BEGIN
    SET NOCOUNT ON
    INSERT INTO dbo.Test (CreationDate, Value) VALUES (@CreationDate, @Value)
    SELECT TestId = SCOPE_IDENTITY()
END

并修改了页面中的ADODB.Command位,如下所示:

Dim command : Set command = Server.CreateObject("ADODB.Command")
command.ActiveConnection = connection
command.CommandText = "dbo.TestInsertion"
command.CommandTimeout = 5
command.CommandType = adCmdStoredProc
command.Parameters.Refresh
command.Parameters(1).Value = Date
command.Parameters(2).Value = value
command.Prepared = True
command.Execute
Set command = Nothing

并且有效。

是否可以在经典ASP中为TEXT指定ADODB.Command参数而无需借助存储过程?

1 个答案:

答案 0 :(得分:8)

尝试将adLongVarChar常量传递给文本数据类型。

使用adLongVarChar您还需要指定value的大小,否则您将收到Parameter object is improperly defined. Inconsistent or incomplete information was provided.错误。

command.Parameters.Append command.CreateParameter("Value", adLongVarChar, adParamInput, Len(value), value)

查看一些ADO Data Type Mappings