繁体   English   中英

在经典 ASP 中执行 SQL 存储过程

[英]Execute SQL Stored procedure in Classic ASP

下面是ASP页面,这个页面的onload我需要执行一个存储过程。 我需要将一个参数传递给这个存储过程。 我从会话中获得的此参数的值。 存储过程应该将记录插入表中,我不需要它返回任何值。 无论我做什么,我都无法从 ASP 页面执行它。 请帮忙。 我在这里做错了什么?

<% @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. 您应该验证参数.....它存在,并且它是预期数据类型的正确值
  2. 您需要某种错误警报/日志记录。 以下是您修改的代码。

  3. 混合使用ADODB.Connection代码(数据层)和 html/asp(表示层)让我的眼睛受伤。

我在下面所做的更改来自

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>

追加

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)

其中 connection_string := 作为 ODBC 源给出的 ODBC 连接字符串; 5 代表任何数字参数的示例。 取自工作 ASP 文件。

最终代码:

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

我收到以下错误,请注意错误描述末尾有 Session 变量 (strStudentID)。

  1. 我在生产服务器上遇到的这个错误错误号是:-2147217911 给出的描述是:[Microsoft][SQL Server Native Client 11.0][SQL Server] 915111111

  2. 此错误发生在非生产服务器上。 错误编号为:-2147217911 给出的描述为:[Microsoft][ODBC SQL Server Driver][SQL Server]915111111

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM