这是我的'代码':
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。帮帮我们?
答案 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