0

所以这是我的功能..它工作得很好,除了它过滤每个包含数字 3 的单元格,如 300 313 30 43 但我希望它只过滤只有单个数字 3 的单元格值!

Private Sub LinkLabel4_Click(sender As Object, e As System.EventArgs) Handles LinkLabel4.Click

    Dim search As String
    search = "SELECT * FROM products WHERE rank LIKE '%" & 3 & "%'"

    Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\db.mdb"
    Dim adapter As New OleDbDataAdapter(search, connectionString)

    Dim dt As New DataTable("products")
    adapter.Fill(dt)
    DataGridView1.DataSource = dt
End Sub

另一件事是选择语句用于过滤2个指定数字之间的值,比如4到16之间谢谢

4

1 回答 1

1

如果rank是数字数据类型,那么您的查询应该是这样的

search = "SELECT * FROM products WHERE rank = 3"

在第二种情况下

search = "SELECT * FROM products WHERE rank >=4 AND rank <= 16"

要使您的搜索参数化,您可以编写这样的方法

Sub RankFilter(ByVal rankLowValue As Integer, Optional ByVal rankHiValue As Integer = -1)

    Dim search As String 
    if rankHiValue = -1 then
        search = "SELECT * FROM products WHERE rank = ?" 
    else
        search = "SELECT * FROM products WHERE rank >= ? and rank <= ?" 

    Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\db.mdb" 

    Dim adapter As New OleDbDataAdapter(search, connectionString)
    adapters.SelectCommand.Parameters.AddWithValue("@rankLo", rankLowValue)
    if rankHiValue <> -1 then
         adapters.SelectCommand.Parameters.AddWithValue("@rankHi", rankHiValue)
    end if

    Dim dt As New DataTable("products") 
    adapter.Fill(dt) 
    DataGridView1.DataSource = dt 
end sub
于 2012-07-08T20:42:52.563 回答