【问题标题】:How to filter ComboBox item according by TextBox1 (VB.NET)?如何根据 TextBox1 (VB.NET) 过滤 ComboBox 项?
【发布时间】:2015-02-21 03:13:02
【问题描述】:

我希望在输入 TextBox1 时,combobox1 根据 TextBox1 进行过滤。 我该怎么做? 这是我的代码:

    >>>The TextBox1 entered like this:
Textbox1.text = 2015-02       


    >>>This is load the ComboBox
    cmd = New OleDbCommand("Select * from TABLE where COL1 = TextBox1.Text", Conn)
    rd = cmd.ExecuteReader
    Do While rd.Read
        COMBOBOX1.Items.Add(rd.Item(0))
    Loop

    >>>It work when I write like this:
    cmd = New OleDbCommand("Select * from TABLE where COL1 = '2015-02'", Conn)
    rd = cmd.ExecuteReader
    Do While rd.Read
        COMBOBOX1.Items.Add(rd.Item(0))
    Loop


   >>>  But i wanna the COL1 according by TextBox1.Text

【问题讨论】:

    标签: vb.net combobox connection


    【解决方案1】:

    把你的命令行改成

    cmd = New OleDbCommand("Select * from TABLE where COL1 = "' & TextBox1.Text & "'", Conn)
    

    它会起作用。根据您的代码,该命令将检查 COL1 中的“textbox.text”

    根据收到的评论,使用参数化查询你的命令行看起来像

    cmd = New OleDbCommand("Select * from TABLE where COL1 = @text", Conn)
    cmd.Parameters.AddWithValue("@text",TextBox1.Text)
    

    【讨论】:

    • 这段代码容易受到SQL注入攻击,你应该使用Parameterized Queries来帮助防止这种情况。
    猜你喜欢
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多