问题
Below is the ASP page, onload of this page I need to execute a stored procedure. I need to pass a parameter into this stored procedure. The value for this parameter I am getting from the Session. Stored procedure is supposed to insert a record into a table, and I don't need it to return any value back. I can't get it to execute from the ASP page, no matter what I do. Please help. What am I doing wrong here?
<% @Language=VBScript %>
<%Response.Buffer = true%>
<html>
<head>
</head>
<body>
<!--#include file = "connect.txt" -->
<!--#include file= "adovbs.inc" -->
<%
Set conn = Server.CreateObject("ADODB.Connection")
Set cmd = Server.CreateObject("ADODB.Command")
'this will be my parameter
Dim strID
strID = Session("ID")
ConnectionStr = "Provider=SQLOLEDB;Server=***;Database=***;Uid=***;Pwd=***;"
conn.Open ConnectionStr
With cmd
.ActiveConnection = conn
.CommandType = adCmdStoredProc
.CommandText = "sp_Application_Insert"
.Parameters.Append cmd.CreateParameter("@TUID ", adVarchar, adParamInput, 200, strID)
.Execute
End With
Set cmd = Nothing
conn.close
%>
</body>
</html>
回答1:
- You should validate the parameter.....that it exists, and its a correct value for the expected datatype
You need some kind of error alert/logging. Below is your code modified.
Mixing
ADODB.Connection
code (data layer) and html/asp (presentation layer) makes my eyes hurt.
the changes I made below are from
https://support.microsoft.com/en-us/kb/300043
<% @Language=VBScript %>
<%Response.Buffer = true%>
<html>
<head>
</head>
<body>
<!--#include file = "connect.txt" -->
<!--#include file= "adovbs.inc" -->
<%
On Error Resume Next
Set conn = Server.CreateObject("ADODB.Connection")
Set cmd = Server.CreateObject("ADODB.Command")
'this will be my parameter
Dim strID
strID = Session("ID")
ConnectionStr = "Provider=SQLOLEDB;Server=***;Database=***;Uid=***;Pwd=***;"
conn.Open ConnectionStr
With cmd
.ActiveConnection = conn
.CommandType = adCmdStoredProc
.CommandText = "sp_Application_Insert"
.Parameters.Append cmd.CreateParameter("@TUID ", adVarchar, adParamInput, 200, strID)
.Execute
if Err.Number <> 0 then
Response.Write "An Error Has Occurred on this page!<BR>"
Response.Write "The Error Number is: " & Err.number & "<BR>"
Response.Write "The Description given is: " & Err.Description & "<BR>"
'' you can try the below too
' Set objASPError = Server.GetLastError
' response.write "Category: " & objASPError.Category & _
' "ASPCode: " & objASPError.ASPCode & _
' "Number: " & objASPError.Number & _
' "ASPDescription: " & objASPError.ASPDescription & _
' "Description: " & objASPError.Description & _
' "Source: " & objASPError.Source
end if
End With
Set cmd = Nothing
conn.close
%>
</body>
</html>
APPEND
https://msdn.microsoft.com/en-us/library/ms345484.aspx
GRANT EXECUTE ON OBJECT::dbo.sp_Application_Insert
TO ***; /* where *** is your uid value in your connection string that you did not show */
回答2:
Final Code:
Dim strStudentID
Dim transactionId
strStudentID = Session("ix_National_ID")
transactionId = Session("transactionId")
Session("accesslevel") = ""
session("SessionStatus") = "5"
Set cmd = server.createobject("ADODB.command")
With cmd
.ActiveConnection = con
.CommandType = adCmdStoredProc
.CommandText = "Temple_sp_Application_Insert"
.Parameters.Append cmd.CreateParameter("@TUID ", adVarchar, adParamInput, 200, strStudentID)
.Parameters.Append cmd.CreateParameter("@transactionId ", adVarchar, adParamInput, 200, transactionId)
.Execute
if Err.Number <> 0 then
Response.Write "An Error Has Occurred on this page!<BR>"
Response.Write "The Error Number is: " & Err.number & "<BR>"
Response.Write "The Description given is: " & Err.Description & "<BR>"
end if
End With
Set cmd = Nothing
Set Con = Nothing
con.Close
I am getting the following error, note there is the Session variable (strStudentID) at the end of the error description.
This error I get on the production server The Error Number is: -2147217911 The Description given is: [Microsoft][SQL Server Native Client 11.0][SQL Server]915111111
This error is on the non-production server. The Error Number is: -2147217911 The Description given is: [Microsoft][ODBC SQL Server Driver][SQL Server]915111111
来源:https://stackoverflow.com/questions/39902685/execute-sql-stored-procedure-in-classic-asp