在VB.net中使用while循环打印MS ACCESS表


Print MS ACCESS table with while loop in VB.net

就像我们可以很容易地在php中使用mysql一样,我们执行一个查询,然后在上面运行一个while循环来打印该查询的所有结果记录。我们如何在vb.net(2008)中使用ms access db执行此功能。这是我现在用来打印的代码

sql = "SELECT * From Inventory "
        Dim da As New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Inventory")    
        Label1.Text = ds.Tables("Inventory").Rows(0).Item(2)
        Label2.Text = ds.Tables("Inventory").Rows(1).Item(2)
        Label3.Text = ds.Tables("Inventory").Rows(2).Item(2)

试试这个:

Dim rowCount As Integer = ds.Tables('Inventory').Rows.Count
Dim Labels() As Label = {Label1, Label2, Label3}
foreach label As Label In Labels
  label.Test = ds.Tables('Inventory').Rows(Array.IndexOf(Labels, label)).Item(2)
Next

或者如果您想通过获取表单上的所有标签来动态地获得Labels()数组,请将其替换为上述第2行:

Dim Labels() As Label
For Each ctrl In Form1.Controls
    If TypeOf ctrl Is TextBox Then
        nextNum = Labels.Length +1
        ReDim Preserve Labels(nextNum)
        TeamIndex(nextNum) = ctrl
    End If 
Next