简体   繁体   English

在Visual Basic中循环遍历数据集并将数据存储在数组中

[英]Looping through dataset and storing data in array in visual basic

I am trying to load all the groups into an Array then loop through those groups and see how many assets they have per agent, if assets are more than agents than move them to our dummy group 0. but i am new to this DataSet and TableAdapator . 我正在尝试将所有组加载到Array然后遍历这些组并查看每个代理有多少资产(如果资产多于代理,而不是将它们移动到我们的虚拟组0中。)但是我对此DataSetTableAdapator

basically Group--agent--assets 基本上是组-代理-资产

0--0--10
1--3--3
2--3--5
3--10--15

so group 2 has 2 extra assets i want to move them to our empty group 0 所以第2组有2个额外的资产,我想将它们移到我们的空组0

Please guide 请指导

Dim rs, lines
rs = cn.Execute("select grp from tskmsgrp where listid = 0;")

Dim da As New System.Data.OleDb.OleDbDataAdapter()
Dim da_line As New System.Data.OleDb.OleDbDataAdapter()
Dim ds As New DataSet()
da.Fill(ds, rs, "grp")

MsgBox("There are  total products." & ds.Tables(0).Rows.Count.ToString)
For Each a As DataRow In ds.Tables(0).Rows
    lines = cn.Execute("select count(*) from tsklines where grp ='" & a(0) & "';")
    Dim ds_lines As New DataSet()
    da_line.Fill(ds_lines, lines, "lines")
    MsgBox("Lines for group." & a(0) & " -- " & ds_lines.Tables(0).Rows.Count.ToString)
Next

A couple of points -- you can work out the details. 有两点-您可以确定细节。

  1. You don't need the select statement inside the loop, because you can access the data in the row a. 您不需要循环内的select语句,因为您可以访问a行中的数据。
  2. You don't need to fill a dataset again, just update ds. 您无需再次填充数据集,只需更新ds。 You can update it all at once after the loop is done. 循环完成后,您可以立即更新所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM