我需要在具有一些TEXT
列的SQL Server 2008表上执行INSERT
s,并且我不能为TEXT
列工作的ADODB.Command
参数。
表定义为:
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参数,但无济于事。
在网上,我发现了一篇叫做《如何从ASP调用SQL Server存储过程》的微软知识库文章,它的"方法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.Command
s指定TEXT
参数而不诉诸存储过程?
尝试为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数据类型映射