mysql - 如何使用http请求查询字符串属性来过滤where子句

标签 mysql asp.net vb.net

我需要使用 http 请求查询字符串属性过滤 GetProduct 函数中的 where 子句。我已经在 url 中设置了过滤器。 (例如 Burgers.aspx?filter=burgers')。 Burgers 是数据库表类别的名称(其中 ProductCat = 过滤器)。我知道我需要将参数传递给交互类,因为它不处理请求。请帮忙。

交互类: 公开课互动 继承System.Web.UI.Page ' Sql 命令对象的新实例 私有(private) cmdSelect 作为新的 SqlCommand ' 连接类的实例 私有(private) conIn 作为新连接

区域“菜单功能和子项”

' Set up the SQL statement for finding a Product by ProductCat  
Private Sub GetProduct(ByVal CatIn As String)
    ' SQL String

    Dim strSelect As String
    strSelect = "SELECT *  "
    strSelect &= " FROM Menu "
    strSelect &= " WHERE ProductCat = " 
    strSelect &= "ORDER BY 'ProductCat'"
    ' Set up the connection to the datebase
    cmdSelect.Connection = conIn.Connect
    ' Add the SQL string to the connection
    cmdSelect.CommandText = strSelect
    ' Add the parameters to the connection
    cmdSelect.Parameters.Add("filter", SqlDbType.NVarChar).Value = CatIn

End Sub



'Function to create list of rows and columns
Public Function ReadProduct(ByVal CatIn As String) As List(Of Dictionary(Of String, Object))
    'Declare variable to hold list
    Dim ReturnProducts As New List(Of Dictionary(Of String, Object))
    Try
        Call GetProduct(CatIn)
        Dim dbr As SqlDataReader
        ' Execute the created SQL command from GetProduct and set to the SqlDataReader object
        dbr = cmdSelect.ExecuteReader
        'Get number of columns in current row
        Dim FieldCount = dbr.FieldCount()
        Dim ColumnList As New List(Of String)
        'Loop through all columns and add to list
        For i As Integer = 0 To FieldCount - 1
            ColumnList.Add(dbr.GetName(i))
        Next
        While dbr.Read()
            'Declare variable to hold list
            Dim ReturnProduct As New Dictionary(Of String, Object)
            'Loop through all rows and add to list
            For i As Integer = 0 To FieldCount - 1
                ReturnProduct.Add(ColumnList(i), dbr.GetValue(i).ToString())
            Next
            'Add to final list
            ReturnProducts.Add(ReturnProduct)
        End While
        cmdSelect.Parameters.Clear()
        'Close connection
        dbr.Close()
    Catch ex As SqlException
        Dim strOut As String
        strOut = ex.Message
        Console.WriteLine(strOut)
    End Try
    ' Return the Product object
    Return ReturnProducts
End Function

背后代码:

偏类汉堡 继承System.Web.UI.Page

'String Used to build the necessary markup and product information
Dim str As String = ""
''Var used to interact with SQL database
Dim db As New Interaction

' New instance of the Sql command object
Private cmdSelect As New SqlCommand
' Instance of the Connection class
Private conIn As New Connection

protected 子 printMenuBlock(ByVal ProductName As String) '设置存储产品的变量并从数据库中提取 暗淡产品 = db.ReadProduct(productName)

    'Add necessary markup to str variable, with products information within

    For i As Integer = 0 To product.Count - 1

        str += "<div class='menuItem'>"
        'str += "    <img alt='Item Picture' class='itemPicture' src='" + product(i).ImagePath.Substring(3).Replace("\", "/") + "' />"
        str += "    <div class='itemInfo'>"
        str += "        <h1 class='itemName'>"
        str += "            " + product(i).Item("ProductName") + "</h1>"
        'str += "        <h3 class='itemDescription'>"
        str += "            " + product(i).Item("ProductDescription")
        str += "        <h1 class ='itemPrice'>"
        str += "            " + product(i).Item("ProductPrice") + "</h1>"
        str += "        "
        str += "        </div>"

        str += "    </div>"

    Next

End Sub

''Uses
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Dim v = Request.QueryString("filter")

    'Response.Write("filter is")
    'Response.Write(v)

    Dim value = Request.QueryString("filter")

    'Get string from printMenuBlock method 
    printMenuBlock(str)

    'Print the str variable in menuPlace div
    menuPlace.InnerHtml = str

End Sub

下课

我需要有关如何将 Request.QueryString("filter") 传递给 GetProduct 函数以根据 ProductCategory 按页面进行过滤的指导。提前致谢。

最佳答案

尝试这样的事情:

Dim filter = Request.QueryString("filter")
Dim sqlStr = "Select * From menu Where ProductCat = @filter Order By ProductCat"
cmdSelect.Parameters.Add("filter", SqlDbType.NVarChar).Value = filter

关于mysql - 如何使用http请求查询字符串属性来过滤where子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18643637/

相关文章:

php - 使用 "LIKE"匹配mysql查询中的多个单词

c# - 调整图像大小以适合打印页面的问题

c# - 激活压缩后 .css 文件中的奇怪效果

C# byte* 到 VB.NET BitmapData 扫描线

vb.net - 如何使用模块从线程更新主窗体创建新的主窗体?

PHP从数据库中获取号码

mysql - 在 activerecord 中可能使用连接将两个 SQL 查询减少为一个

mysql - 连接 MYSQL 时强制 .NET 使用特定 IP

c# - 在 Include 语句中使用 Where 子句的 Linq 查询

asp.net - IIS7 的 ASP MVC 路由问题