循环以在数据网格视图第2列中对数据进行计数 [英] Loop to count data in data grid view column 2
本文介绍了循环以在数据网格视图第2列中对数据进行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Dim s As String = dgv.Rows(row).Cells("Column1").Value
cmd = New OleDbCommand("SELECT COUNT(Protocol) As num FROM Raw_Protocol WHERE Protocol = '" & s & "'", connection)
Dr = cmd.ExecuteReader
While Dr.Read()
dgv.Rows.Add()
dgv.Rows(row).Cells("Column2").Value = Dr("num")
datagridview1.Rows(row).Cells("Column4").Value = Dr("num")
row += 1
End While
这是另一个问题,我想让它循环以仅在数据网格视图行第1列获得数据时才对mdb数据库中的数据进行计数,而在数据网格视图行第1列中没有数据时停止计数.可以向我展示我如何更改代码.谢谢
This is another question,i want to make it loop to count the data in the mdb database only when data grid view row column 1 got data and stop when the data grid view row column 1 don''t have data. Can show me how i changed the code.Thanks
推荐答案
不是答案,而是一个强烈的建议:永远不要将您的字符串连接起来以生成SQL命令:它是公开邀请您进行意外或故意的SQL注入攻击,这可能会破坏您的数据库.改用参数化查询:
Not an answer, but a strong suggestion: never, ever concatenate your strings to generate an SQL command: it is an open invitation to an accidental or deliberate SQL injection attack which can destroy your database. Use Parameterized queries instead:
cmd = New OleDbCommand("SELECT COUNT(Protocol) As num FROM Raw_Protocol WHERE Protocol = @PCL", connection)
cmd.Parameters.AddWithValue("@PCL", s);
您能正确解释您想要实现的目标吗?
Can you explain properly what you want to achieve?
如果我能得到您的帮助!
其在datagridview中的列表,其中显示了协议和协议计数,即
协议,计数
a,10
b,2
等等.如果是这样,您可以在查询中解决此问题
If I get what your after!
its a list in a datagridview that shows Protocol and Count of Protocol i.e.
Protocol, Count
a, 10
b, 2
and so on. If so you can solve this in a query
select protocol, count(protocol) from raw_protocol
如本文所示,将查询与数据适配器配合使用, oleDbAdapter [ ^ ]
该示例使用数据集,但您可以根据需要将其替换为数据表.
using the query with a dataadapter as shown in this article, oleDbAdapter[^]
The example uses a dataset but you can just replace it for a datatable if you wish.
这篇关于循环以在数据网格视图第2列中对数据进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文