![](/img/trans.png)
[英]vb.net how do i add a new rows (and update) from datagridview programmatically to sql database
[英]How do I display data from a SQL Server database based on checked box in VB.Net?
我需要根据 VB.Net 中的复选框列表从 SQL Server 检索数据。 当我检查一个项目时,包含该项目的所有行都必须显示在 DataGridView 中。 我该如何做到这一点?
使用此查询:
Select column1, column2
from tablename
where columnname= '"& checkbox.text& "'
那必须过滤您的数据库。
首先,您需要获取复选框中的所有选定项目:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.