VB ASP数组防止多重读取

时间:2016-12-20 01:32:28

标签: asp.net arrays sql-server vb.net

enter image description here

这是我的'代码':

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim arrName As New ArrayList()
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Dim rdr As SqlDataReader
        con.ConnectionString = "Data Source=.\sqlexpress;Initial Catalog=GradingSystemSample;Integrated Security=True;Pooling=False"
        cmd.Connection = con
        con.Open()

        cmd.CommandText = "select numb FROM Table2  "
        rdr = cmd.ExecuteReader
        If rdr.HasRows Then
            While rdr.Read

                arrName.Add(rdr("numb"))
                For Each pno As String In arrName
                    MsgBox(pno)
                Next

            End While

        End If
End sub

我希望我的输出为: 639057318820 639355514108 639056784959

但我的代码输出是: 639057318820 639057318820 639355514108
639057318820 639355514108
639056784959

在阅读下一个索引之前,似乎我的代码正在读取索引0。帮帮我们?

1 个答案:

答案 0 :(得分:0)

更改

If rdr.HasRows Then
    While rdr.Read
        arrName.Add(rdr("numb"))
        For Each pno As String In arrName
            MsgBox(pno)
        Next

    End While
End If

If rdr.HasRows Then
    While rdr.Read
        arrName.Add(rdr("numb"))
    End While
End If
For Each pno As String In arrName
    MsgBox(pno)
Next