[英]Execute SQL Stored procedure in Classic ASP
Below is the ASP page, onload of this page I need to execute a stored procedure.下面是ASP页面,这个页面的onload我需要执行一个存储过程。 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.
无论我做什么,我都无法从 ASP 页面执行它。 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>
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.混合使用
ADODB.Connection
代码(数据层)和 html/asp(表示层)让我的眼睛受伤。
the changes I made below are from我在下面所做的更改来自
https://support.microsoft.com/en-us/kb/300043 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 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 */
Set OBC = Server.CreateObject("ADODB.Connection")
OBC.Open "connection_string"
S = "CALL sp_application_insert(5)"
OBC.Execute(S)
where connection_string := the ODBC connection string given as an ODBC source;其中 connection_string := 作为 ODBC 源给出的 ODBC 连接字符串; 5 stands as an example for any numeric parameter.
5 代表任何数字参数的示例。 Taken from working ASP file.
取自工作 ASP 文件。
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.我收到以下错误,请注意错误描述末尾有 Session 变量 (strStudentID)。
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我在生产服务器上遇到的这个错误错误号是:-2147217911 给出的描述是:[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
错误编号为:-2147217911 给出的描述为:[Microsoft][ODBC SQL Server Driver][SQL Server]915111111
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.