循环通过数据库获取值并将其放在datagridview中的特定位置 [英] Loop through databases get the value and put it in a specific place in datagridview
问题描述
DataGridView
与2列。第一列填充文件夹路径,第二列为空。每个文件夹路径内都是名为DB1的单个数据库。我想从每个数据库中提取1个值(VALUE),然后将该值放在相应的数据库路径旁边,在第二列中。这是我使用的查询 从DB1中选择CODE,VALUE,其中CODE = 2419
我知道如何填充一个 DataGridView
以及如何从数据库中提取1个值,但是,我甚至不知道从哪里开始。
编辑
我设法创建工作循环,但不知道如何将这些值添加到datagridview中的相应位置。
每行作为DataGridViewRow在DataGridView1.Rows
Dim sendtroopid As String
sendtroopid = row.Cells(CODE)。值
On Error Resume Next
Dim FilePath As String = sendtroopid 'DATABASE PATH
使用con作为新的OleDbConnection(Provider = Microsoft.Jet.OLEDB.4.0; Data Source =& FilePath& _
;扩展属性= dBASE IV)
con.Open()
使用cmd作为新的OleDbCommand(SELECT CODE,VALUE FROM DB 1)
cmd.Parameters.AddWithValue(@ CODE,2419)
使用阅读器As OleDbDataReader = cmd.ExecuteReader()
While(reader .Read())
MsgBox(reader(VALUE))
结束而
结束使用
结束使用
结束使用
下一个
编辑2
使用上面的代码,我可以在msgbox中获取所有值。只剩下的是插入另一个循环来将所有这些值(从第0行开始)放到datagridview中。
如果我用
替换msgbox对于i As Integer = 0到DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).Cells(1).Value =(reader(VALUE))
下一个
然后所有行都填充最后一个值(最后一个数据库的值)。
编辑3
我已经改变了
value = reader.Read()
$
与
While(reader.Read())
value = reader(VALUE)
结束而
解决方案我仍然不确定您的查询是如何工作的,但假设这样做,我已经更改了代码。
对于每一行作为DataGridViewRow在DataGridView1.Rows
Dim sendtroopid As String
sendtroopid = row.Cells(CODE)。值
错误恢复下一步
Dim FilePath As String = sendtroopid'DATABASE PATH
Dim value as string ='decl是一个字符串变量来保存结果
使用con As New OleDbConnection(Provider = Microsoft.Jet.OLEDB.4.0; Data Source =& FilePath& _
;扩展属性= dBASE IV)
con.Open()
使用cmd作为新的OleDbCommand(SELECT CODE,VALUE FROM DB1 WHERE CODE = @CODE,con)
cmd.Parameters.AddWithValue(@ CODE,2419)
使用阅读器As OleDbDataReader = cmd.ExecuteReader()
value = reader.Read()
结束使用
结束使用
结束使用
row.Cells(1)= value'将其放在datagridview单元格
下一个
I have a
DataGridView
with 2 columns. The first column is populated with folder paths and the second column is empty. Inside each folder path is a single database named DB1. I would like to extract 1 value (VALUE) from each database and then put that value next to corresponding database path, in the second column. This is the query I am usingSelect CODE, VALUE from DB1 where CODE = 2419
I know how to populate a
DataGridView
and how to extract 1 value from the database, but with this I don't even know where to begin.EDIT
I've managed to create working loop but don't know how to add those values to corresponding places in datagridview.
For Each row As DataGridViewRow In DataGridView1.Rows Dim sendtroopid As String sendtroopid = row.Cells("CODE").Value On Error Resume Next Dim FilePath As String = sendtroopid 'DATABASE PATH Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _ " ;Extended Properties=dBASE IV") con.Open() Using cmd As New OleDbCommand("SELECT CODE, VALUE FROM DB1 WHERE CODE = @CODE", con) cmd.Parameters.AddWithValue("@CODE", "2419") Using reader As OleDbDataReader = cmd.ExecuteReader() While (reader.Read()) MsgBox(reader("VALUE")) End While End Using End Using End Using Next
EDIT 2
With code above I get all values in msgbox. Only thing left is to insert another loop to put all those values (starting with row 0) to datagridview. If I replace msgbox with
For i As Integer = 0 To DataGridView1.Rows.Count - 1 DataGridView1.Rows(i).Cells(1).Value = (reader("VALUE")) Next
then all rows are populated with only last value (value from last database).
EDIT 3 I've changed
value = reader.Read()
with
While (reader.Read()) value = reader("VALUE") End While
解决方案I'm still not sure how your query works, but assuming it does, I've changed your code.
For Each row As DataGridViewRow In DataGridView1.Rows Dim sendtroopid As String sendtroopid = row.Cells("CODE").Value On Error Resume Next Dim FilePath As String = sendtroopid 'DATABASE PATH Dim value as string = "" ' declare a string variable to hold the result Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _ " ;Extended Properties=dBASE IV") con.Open() Using cmd As New OleDbCommand("SELECT CODE, VALUE FROM DB1 WHERE CODE = @CODE", con) cmd.Parameters.AddWithValue("@CODE", "2419") Using reader As OleDbDataReader = cmd.ExecuteReader() value = reader.Read() End Using End Using End Using row.Cells(1) = value ' put it in the datagridview cell Next
这篇关于循环通过数据库获取值并将其放在datagridview中的特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文