使用经典ASP中的参数化查询将长字符串插入Access DB

时间:2013-05-17 12:00:28

标签: asp-classic ado

我正在尝试更新经典的ASP应用程序,作为更新的一部分,我尝试使用字符串连接和参数化查询替换动态SQL。

问题是参数不接受超过210个字符的值。

我收到以下错误...

  

ADODB.Parameter错误'800a0d5d'

     

应用程序使用错误类型的值进行当前操作。

     

/admin/Save_product_subcategories.asp,第30行

我的第一次尝试看起来像这样......

SQLString = "UPDATE Product_SubCategories 
    SET SubCategory=?, Description=? 
    WHERE SubCategoryID=?"

Set courseCommand = Server.CreateObject("ADODB.Command") 
courseCommand.ActiveConnection = objConn
courseCommand.CommandText = SQLString

courseCommand.Parameters(0).value = cleanCategory 
courseCommand.Parameters(1).Value = cleanDescription
courseCommand.Parameters(2).value = cleanSubCategoryId 

我尝试手动设置参数类型并增加参数的大小......

courseCommand.Parameters(1).Type = 203
courseCommand.Parameters(1).Size = 300
courseCommand.Parameters(1).Type = adLongVarWChar

我也尝试使用command.CreateParameter方法创建一个参数但是会出现同样的错误。

param = courseCommand.CreateParameter(,,,,cleanDescription)
'or
param = courseCommand.CreateParameter(,adLongVarWChar,,,cleanDescription)
'or
param = courseCommand.CreateParameter(,adLongVarWChar,,300,cleanDescription)
courseCommand.Parameters(1) = param

我开始认为我唯一的选择就是回到动态sql。

编辑:

我尝试附加参数,而不是使用数组索引将其添加到集合中,但之后没有任何参数可用。

  

提供商错误'80020005'

     

类型不匹配。

     

/admin/Save_product_subcategories.asp,第31行

1 个答案:

答案 0 :(得分:0)

对于其他寻找此问题的人来说,答案是使用Recordset。

SQLString = "select  * from Product_SubCategories where 1=0"

Set rs= Server.CreateObject("ADODB.Recordset") 
rs.open  SQLString , objConn, 1,3 'open as keyset ,lock optimistic that will create empty recordset for you
' Add new record
rs.AddNew
'assign values
rs("SubCategoryID")=cleanSubCategoryId
rs("Description")=cleanDescription
rs("SubCategory")=cleanCategory
' send new record with values to database
rs.Update
'close recordset
rs.close
'destroy recordset object
se rs=nothing