今儿需要帮同事改一个ASP的页面,原本里面好几个SQL查询,在ASP页面里我觉得啰嗦,而且效率也低,于是将查询放到SQLServer的存储过程里,但是以前没有使用过ASP的存储过程,在网上找来例子,照葫芦画瓢:
Function GetCameraUrl(Conn,CameraId)
const adCmdStoredProc = &H0004
const adVarChar = 200
Const adChar = 129
Const adInteger = 3
const adParamUnknown = &H0000
const adParamInput = &H0001
const adParamOutput= &H0002
const adParamInputOutput= &H0002
const adParamReturnValue = &H0004
Dim url
Set cmdTest = Server.CreateObject("ADODB.Command")
With cmdTest
.ActiveConnection = Conn
.CommandText = "[df1500].[dbo].[sp_GetCameraConnectionInfo]"
.CommandType = adCmdStoredProc
'.CommandType = adCmdText
.Parameters.Append cmdTest.CreateParameter("RETURN", adInteger, adParamReturnValue, 4)
.Parameters.Append .CreateParameter("@camera_id", adInteger, adParamInput, 4,CameraId)
.Parameters.Append .CreateParameter("@url", adVarChar, adParamOutput, 1024)
.Execute
url = .Parameters("@url").Value
End With
set cmdTest = Nothing
GetCameraUrl = url
End Function
但是一执行就发现错误:
参数类型不正确,或不在可以接受的范围之内,或与其他参数冲突 错误出现在这一行
.CommandType = adCmdStoredProc
本来很少用ASP,出现这样的提示让人摸不着头脑,我原本以为adCmdStoredProc是内部定义好的东西,网上其他高手说这个东西需要自己定义,哦,明白了。改正后的代码如下:
const adCmdStoredProc = &H0004
const adVarChar = 200
Const adChar = 129
Const adInteger = 3
const adParamUnknown = &H0000
const adParamInput = &H0001
const adParamOutput= &H0002
const adParamInputOutput= &H0002
const adParamReturnValue = &H0004Function GetCameraUrl(Conn,CameraId)
const adCmdStoredProc = &H0004
const adVarChar = 200
Const adChar = 129
Const adInteger = 3
const adParamUnknown = &H0000
const adParamInput = &H0001
const adParamOutput= &H0002
const adParamInputOutput= &H0002
const adParamReturnValue = &H0004Dim url
Set cmdTest = Server.CreateObject("ADODB.Command")
With cmdTest
.ActiveConnection = Conn
.CommandText = "[df1500].[dbo].[sp_GetCameraConnectionInfo]"
.CommandType = adCmdStoredProc
'.CommandType = adCmdText
.Parameters.Append cmdTest.CreateParameter("RETURN", adInteger, adParamReturnValue, 4)
.Parameters.Append .CreateParameter("@camera_id", adInteger, adParamInput, 4,CameraId)
.Parameters.Append .CreateParameter("@url", adVarChar, adParamOutput, 1024)
.Execute
url = .Parameters("@url").Value
End With
set cmdTest = Nothing
GetCameraUrl = urlEnd Function