[英]vb.net/mysql show more then 1 row in TextBox
I just started messing around with Visual basic (vb.net) and am trying to show more then 1 database row in a TextBox, so far I have this: 我刚开始弄乱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
which connects to a database successfully but but it only outputs 1 row, how can I get it to output more? 哪个成功连接到数据库,但只输出1行,如何获取更多行呢?
You need a loop reading data and storing line after line in a StringBuilder. 您需要一个循环读取数据并在StringBuilder中逐行存储。
Then, when exiting from the reading loop set the Text property of your textbox 然后,从阅读循环退出时,设置文本框的Text属性
Dim sb as StringBuilder = new StringBuilder()
While myData.Read()
sb.AppendLine(myData("foo1") & myData("foo2"))
End While
Viewer.Text = sb.ToString
and, of course, your textbox should have the MultiLine
property set to True 当然,您的文本框应将MultiLine
属性设置为True
Apart from this direct answer to your question, your code should be changed to dispose the connection and the datareader after use, I have also removed the DataAdapter because is not needed here 除了直接回答您的问题之外,还应在使用后更改您的代码以处理连接和数据读取器,我也删除了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
You need some kind of loop. 您需要某种循环。 I would also use the Using
statement to ensure that all unmanaged resources are disposed even in case of an exception(it also closes the connection): 我还将使用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
However, if you want to show multiple records i would recommend a ListBox
instead. 但是,如果要显示多个记录,我会推荐一个ListBox
。 It's more efficient with many items and it also separates them logical from each other. 它在处理许多项目时效率更高,并且将它们逻辑上分开。
( just replace viewer.Text &= line
with ListBox1.Items.Add(line)
) (只需将viewer.Text &= line
替换为ListBox1.Items.Add(line)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.