0

我刚开始使用 Visual basic (vb.net) 并试图在 TextBox 中显示超过 1 个数据库行,到目前为止我有这个:

Private Sub foobox_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim conn As MySqlConnection

        conn = New MySqlConnection
        conn.ConnectionString = connStr

        Try
            conn.Open()
        Catch myerror As MySqlException
            MsgBox("No connection")
        End Try

        Dim myAdaptor As New MySqlDataAdapter

        Dim sqlquery = "SELECT * FROM foo ORDER BY id DESC"
        Dim myCommand As New MySqlCommand()
        myCommand.Connection = conn
        myCommand.CommandText = sqlquery

        myAdaptor.SelectCommand = myCommand
        Dim myData As MySqlDataReader
        myData = myCommand.ExecuteReader()

        If myData.HasRows Then
            myData.Read()
            Viewer.Text = myData("foo1") & myData("foo2")

        End If

        myData.Close()
        conn.Close()

    End Sub

它成功连接到数据库,但只输出 1 行,我怎样才能让它输出更多?

4

2 回答 2

2

您需要一个循环读取数据并在 StringBuilder 中逐行存储。
然后,当退出阅读循环时,设置文本框的 Text 属性

    Dim sb as StringBuilder = new StringBuilder()
    While myData.Read()
        sb.AppendLine(myData("foo1") & myData("foo2"))
    End While
    Viewer.Text = sb.ToString        

当然,您的文本框应该将MultiLine属性设置为 True

除了对您的问题的直接回答之外,您的代码应该在使用后更改为处理连接和数据读取器,我还删除了 DataAdapter 因为这里不需要

    Using conn = New MySqlConnection(connStr)
        Try
            conn.Open()
        Catch myerror As MySqlException
            MsgBox("No connection")
        End Try

        Dim sqlquery = "SELECT * FROM foo ORDER BY id DESC"
        Dim myCommand As New SqlCommand(sqlquery, conn)
        Using myData = myCommand.ExecuteReader()
            Dim sb as StringBuilder = new StringBuilder()
            While myData.Read()
                sb.AppendLine(myData("foo1") & myData("foo2"))
            End While
            Viewer.Text = sb.ToString        
        End Using
    End Using
于 2013-03-04T21:42:30.863 回答
1

你需要某种循环。我还将使用该Using语句来确保即使在出现异常的情况下也处理所有非托管资源(它也会关闭连接):

Using conn As New MySqlConnection(connStr)
    Using myCommand As New MySqlCommand("SELECT * FROM foo ORDER BY id DESC", conn)
        Try
            conn.Open()
            Using myData = myCommand.ExecuteReader()
                If myData.HasRows Then
                    While myData.Read()
                        Dim line = String.Format("{0}{1}{2}",
                                                 myData.GetString(myData.GetOrdinal("foo1")),
                                                 myData.GetString(myData.GetOrdinal("foo1")),
                                                 Environment.NewLine)
                        viewer.Text &= line
                    End While
                End If
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Using
End Using

但是,如果您想显示多条记录,我会推荐一个ListBox。它对许多项目更有效,并且还将它们在逻辑上彼此分开。

(只需替换viewer.Text &= lineListBox1.Items.Add(line)

于 2013-03-04T21:50:32.153 回答