0

我有这段代码,我想通过单击更新按钮来显示数据库中的所有数据,但现在它只显示最后一行。我以为 While 会重复自己并发布每一行,但是..是的..谢谢。

Private Sub update_Click(sender As Object, e As EventArgs) Handles update.Click
Dim show_rows As String
Dim Conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\...\mydb.mdb")
Conn.Open()
Dim command As OleDbCommand = New OleDbCommand("SELECT * FROM dreamware_db", Conn)
Dim read As OleDbDataReader = command.ExecuteReader()
If read.HasRows Then
While read.Read()
show_rows = read.Item("text")
post_dreams.Text = "Show:" & show_rows
End While
End If
Conn.Close()
End Sub
4

2 回答 2

1

它只显示最后一行,因为您show_rows通过循环重置了每次的值。

你可以这样做:

show_rows = show_rows & read.Item("text") & vbCrLf
于 2013-11-13T20:27:08.140 回答
0

改用StringBuilder类:

Private Sub update_Click(sender As Object, e As EventArgs) Handles update.Click
    Dim show_rows As New StringBuilder()
    Dim Conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\...\mydb.mdb")
    Conn.Open()
    Dim command As OleDbCommand = New OleDbCommand("SELECT * FROM dreamware_db", Conn)
    Dim read As OleDbDataReader = command.ExecuteReader()

    If read.HasRows Then
        While read.Read()
            show_rows.AppendLine(read.Item("text"))
        End While
    End If
    Conn.Close()

    ' Do something with show_rows here
    post_dreams.Text = "Show:" & show_rows.ToString()
End Sub
于 2013-11-13T21:02:31.840 回答