【问题标题】:Adding TEXT parameters to ADODB.Command without using stored procedures在不使用存储过程的情况下将 TEXT 参数添加到 ADODB.Command
【发布时间】:2012-01-19 09:04:32
【问题描述】:

我需要在带有一些 TEXT 列的 SQL Server 2008 表上执行 INSERTs,但我不能让 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 中为 ADODB.Commands 指定 TEXT 参数而不使用存储过程?

【问题讨论】:

    标签: sql-server asp-classic parameters adodb


    【解决方案1】:

    尝试为 text 数据类型传递 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

    【讨论】:

    • 优秀,adLongVarChar 工作;但是我必须指定参数的长度,否则我会得到Parameter object is improperly defined. Inconsistent or incomplete information was provided. 错误。我已经相应地更新了你的答案。
    猜你喜欢
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 2017-01-24
    相关资源
    最近更新 更多