mysql - ASP/MySQL - 参数化查询

标签 mysql sql asp-classic

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

我在这里做错了什么,想知道它是什么。在我的示例中,您可以看到两个查询。如果我省略最后一个查询(在“////////”行下),则该脚本将起作用。添加最后一个查询后,我收到以下错误:

"Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done."

我真的不知道我做错了什么。我用谷歌搜索了这个错误,它说了一些关于数据类型的内容,但它没有在我空洞的头脑中注册!

当我处理多个查询时,我是否在正确的位置声明了参数 (.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

最佳答案

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

要解决该问题,请添加:

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

在您的////评论之后,事情应该开始工作。

关于mysql - ASP/MySQL - 参数化查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7761996/

相关文章:

sql - Oracle - 更新连接 - 非键保留表

php - 向表中插入多行

php - MySQL插入改变编码

mysql - LIKE 模式中的逻辑运算符

sql - 我出错了--- -“Invalid truncate option - missing STORAGE keyword”

asp-classic - 没有从 PayPal 取回数据

PHP - PDO 引用是否免受 SQL 注入(inject)攻击?

php - PHP-无法连接到MySQL数据库

asp-classic - 带参数的 CLASSIC ASP INSERT 语句

javascript - 在没有 ID 的情况下访问类中的按钮值