VB:使用MySQL DB中的数据填充多个文本框

时间:2013-12-21 19:52:35

标签: mysql vb.net

所以,我有这个程序,它必须存储来自用户的许多不同信息,并在表单加载时通过Textboxes,Numerics等显示它。起初我觉得这很容易,但是一旦我开始编写代码,我就认为如果我能做到简单的方法(我知道的方式),我将不得不编写数百个Subs,每个都有一个MySQL查询和然后逐个将值分配给各自的文本框,组合框等

那么,如何从MySQL DB中的多行中提取数据,然后将每行中的数据分配给文本框?

这就是我现在所拥有的,它可以正常工作,但仅用于从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

1 个答案:

答案 0 :(得分:1)

据我了解你的问题,你可以使用数据表:

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