【发布时间】:2017-02-21 21:16:32
【问题描述】:
我的 Web 应用程序包含一个 URL 列表,当单击这些 URL 时,它们会从数据库中检索信息。该应用程序使用以下非存储过程方法运行良好:
代码:
Retrieve the connection string stored in the Web.config file.
Dim connectionString As [String] = ConfigurationManager.ConnectionStrings("dbName_dbConnectionString").ConnectionString
Dim connection As New SqlConnection(connectionString)
connection.Open()
Dim mySQLQuery As String
Dim vID As String
vID = Request.QueryString("submittalList")
mySQLQuery = "SELECT submittalsInfo.CategoryID, submittalsInfo.url, submittalsInfo.headerName, submittalsInfo.FileName FROM submittals INNER JOIN submittalsInfo ON submittals.CategoryID = submittalsInfo.CategoryID WHERE (submittalsInfo.CategoryID = submittals.CategoryID) AND submittalsInfo.CategoryID = '" & vID & "' AND submittalsInfo.isActive = '1' ORDER BY submittalsInfo.FileName"
Dim myCommand As New SqlCommand(mySQLQuery, connection)
Dim myReader1 As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
While (myReader1.Read())
showSubHeader.Text = myReader1.GetString(2).ToString + ":"
showurls.Text = showurls.Text + "<div>" + myReader1.GetString(1).ToString + "</div>"
End While
connection.Close()
...我正在尝试使用存储过程产生相同的结果。以下是我的设置:
存储过程:
ALTER PROCEDURE [dbo].[showSubmittals]
AS
Begin
SELECT submittalsInfo.CategoryID, submittalsInfo.url, submittalsInfo.headerName, submittalsInfo.FileName FROM submittals INNER JOIN submittalsInfo ON submittals.CategoryID = submittalsInfo.CategoryID WHERE (submittalsInfo.CategoryID = submittals.CategoryID) AND submittalsInfo.CategoryID = '" & vID & "' AND submittalsInfo.isActive = '1' ORDER BY submittalsInfo.FileName
END
...这是我用来调用它的代码:
'Retrieve the connection string stored in the Web.config file.
Dim connectionString As [String] = ConfigurationManager.ConnectionStrings("webname_dbConnectionString").ConnectionString
Dim connection As New SqlConnection(connectionString)
Dim myCommand As New SqlCommand()
myCommand.CommandType = CommandType.StoredProcedure
myCommand.CommandText = "showSubmittals"
myCommand.Connection = connection
Try
connection.Open()
Dim myReader1 As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
While (myReader1.Read())
showSubHeader.Text = myReader1.GetString(2).ToString + ":"
showurls.Text = showurls.Text + "<div>" + myReader1.GetString(1).ToString + "</div>"
End While
Catch ex As Exception
Throw ex
Finally
connection.Close()
connection.Dispose()
End Try
...遇到的问题是单击 URL 时页面(URL)没有呈现任何结果。下面显示了我的 URL 是如何设置的,我试图在页面 showSubmittals.aspx 中呈现结果,其中包含我上面解释的后端代码(我的方法):
<asp:HyperLink ID="HyperLink1" runat="server" Text="Accessories" NavigateUrl="~/showSubmittals.aspx?submittalList=1" CssClass="submittalsLinks" />
我能否就如何使用存储过程方法完成此任务获得一些帮助?提前致谢
【问题讨论】:
-
存储过程在SSMS中运行时是否返回数据?来自 SQL 服务器的存储过程应该像查询一样返回数据。 SQL Server 不像 Oracle。它不需要参考光标。
-
您是否尝试调试并查看命令执行后是否执行了while循环?
-
为什么你的存储过程有字符串连接的vb语法?您需要将
vId作为存储过程中的参数并从代码中传递。 -
那个存储过程甚至不会保存。运行
alter命令时肯定会出错。 -
因为 SP 甚至没有被保存,所以你的一切都失败了。因此,当您查看 F12 控制台时,您会看到对 URL 的请求引发了 500 错误。
标签: asp.net vb.net stored-procedures