【问题标题】:How do I display data from a SQL Server database based on checked box in VB.Net?如何根据 VB.Net 中的复选框显示 SQL Server 数据库中的数据?
【发布时间】:2018-02-27 04:48:39
【问题描述】:

我需要根据 VB.Net 中的复选框列表从 SQL Server 检索数据。当我检查一个项目时,包含该项目的所有行都必须显示在 DataGridView 中。我该如何做到这一点?

【问题讨论】:

标签: sql sql-server vb.net


【解决方案1】:

使用这个查询:

Select column1, column2 
from tablename 
where columnname= '"& checkbox.text& "'

那必须过滤你的数据库。

【讨论】:

  • 哦,对不起,我的错。我已经修好了。
  • 用大写字母写作相当于对你的读者SHOUTING,这简直是粗鲁 - 不要这样做 - 请立即修复你的帖子
  • 对不起,我不会再这样做了
【解决方案2】:

首先,您需要获取复选框中所有选中的项目:

For Each checkBox As CheckBox In chkbxlst.Items
    If (checkBox.Checked = True) Then
        'its selected
    End If
Next

接下来,编写一个 SQL 查询,

select * from tablename where column in ({0})

我们可以采用参数化方式或非参数化方式。建议参数化以避免SQL注入攻击。

Dim query as String = "select * from tablename where column in ({0})"
Dim i as Int = 0
Dim sqlCommand = new SqlCommand()
sqlCommand.Connection = connection
sqlCommand.CommandType = CommandType.Text
Dim sb = new new List<string>()
For Each checkBox As CheckBox In chkbxlst.Items
    If (checkBox.Checked = True) Then
        Dim paramName as String = "paramName" + i
        sb.Append(paramName)
        sqlCommand.Parameters.AddWithValue(paramName, checkBox.Text)
    End If
Next
sqlCommand.CommandText = String.Format(query, string.Join(",", sb))
'..execute the command and get response

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-20
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 2021-04-08
    • 2017-02-07
    相关资源
    最近更新 更多