VB.net - 显示数据库中的所有数据

时间:2013-11-13 20:22:40

标签: database vb.net

我有这个代码,我想在点击我的更新按钮时显示数据库中的所有数据,但是现在它只显示最后一行。 我以为虽然会重复自己并发布每一行,但是..是啊.. 感谢。

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

2 个答案:

答案 0 :(得分:1)

它只显示最后一行,因为每次循环都会重置show_rows的值。

你可以这样做:

show_rows = show_rows & read.Item("text") & vbCrLf

答案 1 :(得分: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