Visual Basic遍历每一行并在不同的标签中写入每行的第一列的值

时间:2015-05-07 23:35:10

标签: mysql visual-studio

我设法写了label1中第一行的值,但我不知道为什么我无法获得label2中第二行的值。这是我能够走多远

    myCommand.CommandText = "SELECT allText FROM tbltest"
    myDatareader = myCommand.ExecuteReader

    Do While myDatareader.Read()
        label1.Text = myDatareader(0).ToString()
        label2.Text = myDatareader(1).ToString()
    Loop

PS!我得到这个错误"索引超出了数组的范围"。我做错了什么?

PSS!在Alex的帮助下,我写了这些并解决了这个问题。 它的作用是,它读取值并将它们作为标签写入Panel中。

溶液

    Public Sub getit()
        Dim count As Integer = 0

        myCommand.CommandText = "SELECT allText FROM tbltest"
        myDatareader = myCommand.ExecuteReader

        Do While myDatareader.Read
            count += 1
            show(myDatareader(0).ToString, count)
        Loop
    End Sub

    Public Sub show(ByVal name As String, ByVal count As Integer)
        Dim allTextlabel As New Label()

        allTextlabel.Location = New Point(10, (45 * (count)) + 1)
        allTextlabel.Size = New Size(100, 25)
        allTextlabel.Name = "lbl_" & name.ToString
        allTextlabel.Text = name.ToString

        Panel1.Controls.Add(allTextlabel)
    End Sub

1 个答案:

答案 0 :(得分:0)

由于您只需要myCommand.CommandText = "SELECT allText FROM tbltest"一列allText myDatareader(1),所以myDatareader(0)只有Dim Phrase() as string i=0 Do While myDatareader.Read() i=i+1 Phrase(i) = myDatareader(0).ToString(); Loop label1.Text = Phrase(1).ToString() label2.Text = Phrase(2).ToString() label3.Text = Phrase(3).ToString() label4.Text = Phrase(4).ToString() label5.Text = Phrase(5).ToString() 只有第一个索引为0 var a = 0; function add(name) { if (a <= 10) { a++; var parent = name.parentNode; var divtest = document.createElement("div"); divtest.setAttribute("id", a); divtest.innerHTML = "test" + a; if(a>1){ var lastdiv = document.getElementById(a-1); parent.removeChild(lastdiv); } parent.appendChild(divtest); } }

更新我在VB中不是很强大,但您可以尝试:

var a = 0;

function add(name) {
    if (a <= 10) {
        a++;
        var parent = name.parentNode;
        var divtest = document.createElement("div");
        divtest.setAttribute("id", a);
        divtest.innerHTML = "test" + a;
        parent.appendChild(divtest);
    }
}