vb.net VB:用 MySQL DB 中的数据填充多个文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20722928/
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: Fill multiple textboxes with data from MySQL DB
提问by andre_ss6
So, I have this program which has to store a lot of different info from users and shows it through Textboxes, Numerics, etc when the Form loads. At first I thought it would be easy, but as soon as I started writing the code I figured that if I would do the easy way (the way I know) I would have to write hundreds of Subs, each one with a MySQL query and then assign, one by one, the values to their respective textboxes, comboboxes, etc.
所以,我有这个程序,它必须存储来自用户的许多不同信息,并在表单加载时通过文本框、数字等显示它。起初我认为这很容易,但是当我开始编写代码时,我想如果我采用简单的方法(我所知道的方式),我将不得不编写数百个 Sub,每个都带有一个 MySQL 查询和然后将值一一分配给它们各自的文本框、组合框等。
So, how can I pull data from multiple rows from a MySQL DB and then assign the data from each one of those rows to textboxes?
那么,如何从 MySQL 数据库中的多行中提取数据,然后将这些行中的每一行中的数据分配给文本框?
This is what I have now, which works fine, but only for getting one single value from the DB:
这就是我现在所拥有的,它工作正常,但只能从数据库中获取一个值:
Imports MySql.Data.MySqlClient
Public Class GetInfo
Public Shared Sub Run()
Dim reader As MySqlDataReader
Dim result As String
Dim Query_Read As String = "Select Nome FROM dk_db_sql_yog." & Username
Dim Cmd_Read_Name As New MySqlCommand(Query_Read)
Cmd_Read_Name.Connection = Connect
reader = Cmd_Read_Name.ExecuteReader()
If reader.Read() Then
If reader.IsDBNull(0) Then
result = ""
Else
result = reader.GetString(0)
End If
End If
Form1.Name_Textbox.Text = result
reader.Close()
End Sub
End Class
回答by Ruben_PH
As I understand your question, you may use a datatable:
据我了解您的问题,您可以使用数据表:
Dim reader As MySqlDataReader
Dim result As New Datatable
Dim Query_Read As String = "Select Nome, Nome1, Nome2 FROM dk_db_sql_yog." & Username
Dim Cmd_Read_Name As New MySqlCommand(Query_Read)
Cmd_Read_Name.Connection = Connect
result.Load(Cmd_Read_Name.ExecuteReader)
With Form1
For Each dtrow AS DataRow in result.rows
.Name_Textbox1.Text = dtrow(0)
.Name_Textbox2.Text = dtrow(1)
.Name_Textbox3.Text = dtrow(2)
Next
End With

