Vb阵列可防止多次读取 [英] Vb array prevent multi reading

查看:91
本文介绍了Vb阵列可防止多次读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有手机号码的数据库现在我想将它们放在数组列表中并逐个显示它们。



我希望我的输出为: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

推荐答案

首先,没有必要

First, there is no need to
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.


我建​​议以这种方式纠正:

I'd suggest to correct this way:
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





为什么?请阅读: SqlDataReader.Read方法(System.Data.SqlClient) [ ^ ]


这篇关于Vb阵列可防止多次读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆