使用Hashtable时Visual Basic 2010小错误

时间:2012-04-24 11:46:49

标签: visual-studio-2010

 Dim cmd1 As New OleDbCommand("Select * FROM Membros WHERE NFamilia=@nfamilia", con)
    cmd1.Parameters.Add("@nfamilia", OleDbType.VarChar).Value = nfamilia.Text

    con.Open()
    Dim dr2 As OleDbDataReader = cmd1.ExecuteReader()
    While (dr2.Read())
        Dim n As Integer = dr2(1)
        Dim nome As String = dr2(2)
        For i = 1 To 10
            table.Item(n).text = nome
        Next
    End While
    con.Close()

我有这个代码会在Hashtable中插入数据但我有一个问题dr2(1)它是一个自动增量值,我得到一些像this

如果我将代码更改为:

            Dim n As Integer 
                n=1
                Dim nome As String = dr2(2)
                For i = 1 To 10
                    table.Item(n).text = nome
                    n=n+1
                Next
            End While

我得到this

如果我以这种方式制作代码,我会得到与上面相同的

        While (dr2.Read())

        Dim nome As String = dr2(2)
        For i = 1 To 10
            table.Item(i).text = nome
        Next
End While

1 个答案:

答案 0 :(得分:1)

你能尝试一下吗?

i = 1
While (dr2.Read())
    table.Item(i).text = dr2(2)
    i = i + 1
End While