问题描述
我有手机号码的数据库现在我想将它们放在数组列表中并逐个显示它们。
我希望我的输出为:639057318820 639355514108 639056784959
但是我的代码输出是:639057318820 639057318820 639355514108
639057318820 639355514108
639056784959
我的尝试:
i have database with mobile numbers now i want to put them in array list and show them one by one.
I want my output to be: 639057318820 639355514108 639056784959
but my code output is: 639057318820 639057318820 639355514108
639057318820 639355514108
639056784959
What I have tried:
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
推荐答案
rdr.HasRows
as
as
rdr.Read
将返回false当没有行时。
For Each的位置值得怀疑。为什么把它放在While里面,这就是造成多重阅读的罪魁祸首。它应该放在End While之后。
will return false when there is no row.
The location of the For Each is questionable. Why put it inside the While and that is the culprit that causes "multi reading". It should be placed after the End While.
rdr = cmd.ExecuteReader
'If rdr.HasRows Then
While rdr.Read
arrName.Add(rdr("numb"))
'For Each pno As String In arrName
MsgBox(rdr("numb"))
'Next
End While
为什么?请阅读: []
这篇关于Vb阵列可防止多次读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!