在Classic ASP中执行SQL存储过程

时间:2016-10-06 17:55:57

标签: sql-server stored-procedures vbscript asp-classic

下面是ASP页面,这个页面的onload我需要执行一个存储过程。我需要将参数传递给此存储过程。我从Session获取的此参数的值。存储过程应该将记录插入表中,我不需要它来返回任何值。 无论我做什么,我都无法从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>

3 个答案:

答案 0 :(得分:1)

  1. 您应该验证参数.....它是否存在,以及它是预期数据类型的正确值
  2. 您需要某种错误提醒/日志记录。以下是您修改的代码。

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

  4. 我在下面做的更改来自

    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 */
    

答案 1 :(得分:0)

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 文件。

答案 2 :(得分:-1)

最终守则:

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驱动程序] [SQL Server] 915111111