我需要显示来自MySQL的一组数据,例如DataGridView
或ListView
的功能,但是使用多个TextBox
控件。我的工作成果是列中的所有数据都在一个TextBox
上。我不知道怎么做循环。
代码:
Imports System.Data
Imports MySql.Data.MySqlClient
Public Class Form1
Dim con As MySqlConnection = New MySqlConnection("data source=localhost; user id=root; database=abc; password=")
Dim query As String = "SELECT id,timein,timeout FROM emp"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using myCommand As New MySqlCommand("SELECT * FROM emp ORDER BY id ASC", con)
Try
con.Open()
Using myData = myCommand.ExecuteReader()
If myData.HasRows Then
While myData.Read()
Dim idline = String.Format("{0}", myData.GetString(myData.GetOrdinal("id")), Environment.NewLine)
Dim inline = String.Format("{0}", myData.GetString(myData.GetOrdinal("timein")), Environment.NewLine)
Dim outline = String.Format("{0}", myData.GetString(myData.GetOrdinal("timeout")), Environment.NewLine)
idbox1.Text &= idline
idbox2.Text &= idline
idbox3.Text &= idline
timein1.Text &= inline
timein2.Text &= inline
timein3.Text &= inline
timeout1.Text &= inline
timeout2.Text &= inline
timeout3.Text &= inline
End While
End If
End Using
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
End Using
con.Close()
End Sub
End Class
答案 0 :(得分:1)
您是否尝试过使用DataTable?我构建了一个表单,将加载时的值从DataTable中拉出,在该表中,您可以指定显示的列和按什么顺序排列。我使用MS VS17 RC中的内置函数来链接表。可能会帮助解决您的问题。
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sqlConn As New SqlConnection("Connection String Info")
Dim cmd As New SqlCommand
Dim dr As New SqlDataAdapter("-Query String-", sqlConn)
Dim dt As New DataTable
Try
If sqlConn.State = ConnectionState.Closed Then
sqlConn.Open()
dr.Fill(dt)
Menu.DisplayMember = "prod_name" --add your columns here
Menu.DataSource = dt
sqlConn.Close()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Order.Clear()
For Each Item As Object In Menu.SelectedItems
Order.AppendText(Item.ToString = Environment.NewLine)
Next
End Sub
答案 1 :(得分:1)
这对我很好。我得到了我需要做的事。谢谢你的想法。 “Imports System.Data 导入MySql.Data.MySqlClient
Public Class Form2 Dim con As MySqlConnection = New MySqlConnection(“data source = localhost; user id = root; database = abc; password =”)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As MySqlConnection = New MySqlConnection("data source=localhost; user id=root; database=abc; password=")
Dim query As String = "SELECT id,timein,timeout FROM emp"
con.Open()
Dim dt As New DataTable
Dim da As New MySqlDataAdapter("SELECT * FROM emp ORDER BY id ASC", con)
da.Fill(dt)
idbox1.Text = dt.Rows(0)("id").ToString()
idbox2.Text = dt.Rows(1)("id").ToString()
idbox3.Text = dt.Rows(2)("id").ToString()
timein1.Text = dt.Rows(0)("timein").ToString()
timein2.Text = dt.Rows(1)("timein").ToString()
timein3.Text = dt.Rows(2)("timein").ToString()
timeout1.Text = dt.Rows(0)("timeout").ToString()
timeout2.Text = dt.Rows(1)("timeout").ToString()
timeout3.Text = dt.Rows(2)("timeout").ToString()
con.Close()
End Sub
结束课程 “