2011-10-14 63 views
0

在下面的示例代碼中,您可以看到我一直試圖在ASP和MySQL中查看參數化查詢。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 

UPDATE:

好我這兩個查詢工作,但我必須設置另一個命令對象,並主動連接,如下圖所示。雖然它起作用,但對於我的連接類型,這是正確的嗎?那麼在每次查詢之後,我是否需要將命令對象設置爲空?

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

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 

回答

2

我相信你的問題是因爲兩個插入語句都使用相同的命令對象。因此,第二個命令將包含兩個參數,這就是我認爲會導致您看到的異常。

要解決該問題,添加:

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

您////評論之後,事情應該開始工作。

+0

哈哈我簡直就是在同一時間發現並更新了我的問題!每次查詢後,我是否需要將最後的'activeConnection'和'command'對象設置爲'Nothing'?我的ADO連接知識是有限的.. – TheCarver

+0

不,不需要對命令對象做任何事情。您可能還想將您的Connection(不是命令)放在Try/Finally中,以確保連接關閉。 –

+0

這很有趣。我可以很好地問你一個這樣的小例子嗎? – TheCarver