2011-06-16 77 views
-1

我以前曾問過類似的問題,雖然我試圖解決我以前的代碼,但現在我得到了一個錯誤「參數是錯誤的類型,超出了可接受的範圍,或相互衝突「。我想傳遞一個字符作爲參數來查詢數據庫,然後使用記錄集打印出一個網頁。我的經典ASP代碼低於傳遞參數到存儲過程中經典的asp

Set objCon = CreateObject("ADODB.Connection") 
Set objRS = CreateObject("ADODB.Recordset") 
set objComm = CreateObject("ADODB.Command") 

objCon.ConnectionString = "Provider=SQLOLEDB.1;Password=xxxx;Persist Security Info=True;User ID=xxxx;Initial Catalog=Movies;Data Source=xxxx-PC" 
objCon.open objCon.ConnectionString 

objComm.ActiveConnection = objCon.ConnectionString 
objComm.CommandText = "Paging_Movies" 
objComm.CommandType = adCmdStoredProc 

Set objParameter = objComm.CreateParameter 
objParameter.Name = "alphaChar" 
objParameter.Type = adChar 
objParameter.Direction = adParamInput 
objParameter.Value = "a" 

objComm.Parameters.Append objParameter 

set objRs = objComm.Execute 

而且我的存儲過程是如下 -

CREATE PROCEDURE Paging_Movies 
@alphaChar char(1) 
AS 
if @alphaChar = '#' 
    select * from Movies where movies like '[^a-z]%' 
else 
    select * from Movies where movies like @alphaChar + '%' 

回答

0

也許你要添加的參數兩次。嘗試更換:

objComm.ActiveConnection = objCon.ConnectionString 
objComm.CommandText = "Paging_Movies" 
objComm.CommandType = adCmdStoredProc 

Set objParameter = objComm.CreateParameter 
objParameter.Name = "alphaChar" 
objParameter.Type = adChar 
objParameter.Direction = adParamInput 
objParameter.Value = "a" 

Set objParameter = objCommand.CreateParameter ("alphaChar", adChar, _ 
    adParamInput, "a") 

objComm.Parameters.Append objParameter 

只:

objCommand.CreateParameter ("alphaChar", 129, 1, 1, "a") 

編輯爲您的意見建議(1)從W3Schools CreateParameter page使用的數值爲adChar(129)和adParamInput

+0

不幸的是我最終的類型不匹配? – 2011-06-16 08:55:26

+0

@ kurupt89:嘗試指定1的長度? [見本手冊頁。](http://www.w3schools.com/ado/met_comm_createparameter.asp) – Andomar 2011-06-16 09:13:19

+0

感謝你的回覆幫助我找到答案。我不得不將adChar和adParamInput更改爲數字表示形式 – 2011-06-16 09:16:58