Ajout de paramètres de TEXTE ADODB.Commande sans l'aide de procédures stockées
J'ai besoin pour effectuer INSERT
s sur un Serveur SQL server 2008 table avec des TEXT
colonnes, et je ne peux pas avoir le ADODB.Command
paramètre pour la TEXT
colonne de travail.
La table est définie comme:
CREATE TABLE dbo.Test (
TestId INT NOT NULL IDENTITY(1, 1)
, CreationDate DATETIME NOT NULL
, Value TEXT NOT NULL
, CONSTRAINT pkTest PRIMARY KEY (TestId)
)
La page de test est comme ceci:
<!-- 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
%>
Quand je l'exécuter, j'ai cette erreur:
ADODB.Command error '800a0d5d'
Application uses a value of the wrong type for the current operation.
/test/default.asp, line 32
La ligne 32 est
command.Parameters.Append command.CreateParameter("Value", adVarChar, adParamInput, 0, value)
J'ai tenté de changer le paramètre de Taille, en vain.
Recherche sur le net j'ai trouvé la Base de Connaissances Microsoft Article intitulé Comment appeler des procédures stockées SQL Server à partir d'ASP, sa "Méthode 1" exemple est intéressant, car il ne déclare les paramètres, mais les lit forme le serveur:
cmd.CommandText = "sp_test"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Refresh
cmd.Parameters(1) = 11
j'ai donc créé une procédure stockée à exécuter l' 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
et modifié le ADODB.Command
peu dans la page comme ceci:
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
et cela a fonctionné.
Est-il possible de spécifier un TEXT
paramètre pour ADODB.Command
s en ASP classique sans avoir recours à des procédures stockées?
OriginalL'auteur Albireo | 2011-12-13
Vous devez vous connecter pour publier un commentaire.
Essayer de passer
adLongVarChar
constante pour le texte type de données.Avec
adLongVarChar
vous devez également spécifier la taille devalue
, sinon vous obtiendrez leParameter object is improperly defined. Inconsistent or incomplete information was provided.
erreur.Voir quelques ADO Mappages de types de Données.
adLongVarChar
œuvres; cependant je dois spécifier le paramètre de la longueur, sinon je vous leParameter object is improperly defined. Inconsistent or incomplete information was provided.
erreur. J'ai mis à jour votre réponse en conséquence.OriginalL'auteur Kul-Tigin