单击URL时,存储过程不会呈现任何数据

时间:2017-02-21 21:16:33

标签: asp.net vb.net stored-procedures

我的网络应用程序包含一个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" />

我可以获得一些帮助,了解如何使用存储过程方法完成此任务吗?提前致谢

1 个答案:

答案 0 :(得分:0)

您的存储过程应如下所示。

ALTER PROCEDURE [dbo].[showSubmittals]
 @vid NVARCHAR (255) // Use appropriate datatype for parameter
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

您需要从代码中调用该过程,如下所示。

Dim connectionString As [String] = ConfigurationManager.ConnectionStrings("webname_dbConnectionString").ConnectionString
Dim connection As New SqlConnection(connectionString)

Dim vID As String

vID = Request.QueryString("submittalList")
Dim myCommand As New SqlCommand()
myCommand.CommandType = CommandType.StoredProcedure
myCommand.CommandText = "showSubmittals"
myCommand.Parameters.Add ("@vid", vID)
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

Pardon语法错误。我是从移动设备发布此答案的。