VB.NET 如何从数据库获取行值到文本框(多行)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28255921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET How to get row values from database to textbox (multiline)
提问by user1947700
I have this code (found it somewhere on the net, maybe even here on stackoverflow)
我有这个代码(在网上的某个地方找到它,甚至可能是在 stackoverflow 上)
Dim SQL As String = "SELECT User FROM T1 WHERE User IS NOT NULL;"
Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\db2.accdb")
Dim command As New OleDbCommand(Sql, con)
con.Open()
Dim reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
TextBox1.Text = reader(0).ToString()
End While
reader.Close()
End Using
In my database I have 3 users but when I am running this code only the last one is showed in the textbox.
在我的数据库中,我有 3 个用户,但是当我运行此代码时,文本框中仅显示最后一个用户。
Let's say my table looks like this
假设我的桌子看起来像这样
User one two three
But my textbox shows only "three"
但我的文本框只显示“三个”
How can I export and show all rows from column User and separate them with new line?
如何从列 User 导出和显示所有行并用新行分隔它们?
And if it is easy I would like to have a code for a list box. You know, like get column name (with sql query) and for every row make a new item in listview.
如果这很容易,我想要一个列表框的代码。您知道,例如获取列名(使用 sql 查询)并为每一行在列表视图中创建一个新项目。
PS: the code works fine, just need to be adjusted. I am working with databases in vb.net for like 3 days, so my question might be easy, but still difficult for me.
PS:代码运行正常,只是需要调整。我在 vb.net 中使用数据库大约 3 天,所以我的问题可能很简单,但对我来说仍然很困难。
Thank you.
谢谢你。
回答by OldProgrammer
While reader.Read()
TextBox1.Text &= reader(0).ToString() & Environment.NewLine
End While
Also make sure to set TextBox1.Multiline = true
还要确保设置 TextBox1.Multiline = true

