ASP / MySQL - 参数化查询

时间:2011-10-14 00:42:09

标签: mysql sql asp-classic

在我下面的示例代码中,您可以看到我一直在尝试在ASP和MySQL中输出参数化查询。

我在这里做错了,想知道它是什么。在我的示例中,您可以看到两个查询。如果我放弃最后一个查询(在'////////行下),这个脚本可以工作。一旦我添加了最后一个查询,我就会收到以下错误:

  

“多步OLE DB操作生成错误。检查每个OLE DB   状态值,如果可用。没有工作。“

我真的不确定我做错了什么。我用谷歌搜索了错误,它说了一些关于数据类型的东西,但它没有在我的空头中注册!

我在正确的位置声明参数(.createParameter),因为我正在处理多个查询?是否必须在所有查询之前声明它们?

我的代码

Set connContent = Server.CreateObject("ADODB.Connection") 
connContent.ConnectionString="...blah..blah..blah..."
connContent.Open

Set cmdContent = Server.CreateObject("ADODB.Command")
Set cmdContent.ActiveConnection = connContent

cmdContent.Prepared = True

Const ad_varChar = 200
Const ad_ParamInput = 1
Const ad_Integer = 3
Const ad_DBDate = 133 
Const ad_DBTimeStamp = 135

theNumber = 23
theText = "Hello there!"
theDate = "2011-10-15"

SQL = " INSERT INTO testTable (integerCol) VALUES (?); "

Set newParameter = cmdContent.CreateParameter("@theNumber", ad_Integer, ad_ParamInput, 50, theNumber)
cmdContent.Parameters.Append newParameter

cmdContent.CommandText = SQL
cmdContent.Execute

' ////////////

SQL = " INSERT INTO testTable (varCharCol) VALUES (?); "

Set newParameter = cmdContent.CreateParameter("@theText", ad_varChar, ad_ParamInput, 50, theText)
cmdContent.Parameters.Append newParameter

cmdContent.CommandText = SQL
cmdContent.Execute

更新

我有两个查询都可以工作,但我必须设置另一个命令对象和活动连接,如下所示。虽然它有效,但这对我的连接类型是否正确?我是否需要在每次查询后将命令对象设置为空?

' ////////////

Set cmdContent = Server.CreateObject("ADODB.Command")
Set cmdContent.ActiveConnection = connContent

SQL = " INSERT INTO testTable (varCharCol) VALUES (?); "

Set newParameter = cmdContent.CreateParameter("@theText", ad_varChar, ad_ParamInput, 50, theText)
cmdContent.Parameters.Append newParameter

cmdContent.CommandText = SQL
cmdContent.Execute

1 个答案:

答案 0 :(得分:2)

我相信你的问题是因为两个insert语句都使用相同的命令对象。因此,第二个命令将包含两个参数,这是我认为会导致您看到的异常。

要解决此问题,请添加:

Set cmdContent = Server.CreateObject("ADODB.Command")
Set cmdContent.ActiveConnection = connContent
在你的////评论之后,事情就应该开始了。