Alb*_*reo 4 sql-server parameters adodb asp-classic
我需要INSERT在SQL Server 2008表上执行带有一些TEXT列的s ,并且我不能让列的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)
)
Run Code Online (Sandbox Code Playgroud)
测试页面是这样的:
<!-- 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
%>
Run Code Online (Sandbox Code Playgroud)
当我执行它时,我收到此错误:
ADODB.Command error '800a0d5d'
Application uses a value of the wrong type for the current operation.
/test/default.asp, line 32
Run Code Online (Sandbox Code Playgroud)
32号线是
command.Parameters.Append command.CreateParameter("Value", adVarChar, adParamInput, 0, value)
Run Code Online (Sandbox Code Playgroud)
我尝试更改Size参数,但无济于事.
环顾网络,我发现微软知识库文章如何从ASP调用SQL Server存储过程,其"方法1"示例很有意思,因为它没有声明参数,而是从服务器读取它们:
cmd.CommandText = "sp_test"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Refresh
cmd.Parameters(1) = 11
Run Code Online (Sandbox Code Playgroud)
所以我创建了一个存储过程来执行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
Run Code Online (Sandbox Code Playgroud)
并修改了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
Run Code Online (Sandbox Code Playgroud)
它起作用了.
是否可以在经典ASP中TEXT为ADODB.Commands 指定参数而无需借助存储过程?
尝试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)
Run Code Online (Sandbox Code Playgroud)
请参阅一些ADO数据类型映射.