Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim conn As New MySqlConnection("server=localhost;user=root;password=Password;database=giordydatabase")
conn.Open()
Dim command As New MySqlCommand("SELECT firstName, lastName FROM student ORDER BY firstName;")
Dim dataSet As New DataSet()
Dim dataAdapter As New MySqlDataAdapter()
dataAdapter.SelectCommand = command
dataAdapter.Fill(dataSet, "student")
Dim dataTable As DataTable = dataSet.Tables("student")
For Each row As DataTable In dataTable.Rows
Dim newStudent As New Student
newStudent.strFirstName = row.Item("firstName")
newStudent.strLastName = row.Item("lastName")
Dim mainStudentList As Object = Nothing
mainStudentList.add(newStudent)
lstStudents.item.add(newStudent.nameConcat)(newStudent.strFirstName, strLastName)
Next
Catch ex As Exception
MsgBox("Error " & ex.ToString())
Finally
conn.Close()
End Try
End Sub
我有一个问题,因为我想将我的数据库连接到列表框,也希望它只提取数据的名字和姓氏数据,并将两者结合起来创建一个名单,供学生选择自己。但上面的代码并不允许我这样做。
请注意,列表框仅供用户选择,一旦他们自己选择,他们就可以使用他们选择的名称使用该应用程序。
答案 0 :(得分:0)
您的问题似乎是您正在设置主要学生列表以输入对象并且没有任何内容......
尝试移动此行
Dim mainStudentList As Object = Nothing
在嵌套的位置移动它,以便它是代码块中的第一行...
此外,请将其更改为...
,而不是您当前在该行中获得的内容Dim mainStudentList As Generic.List(Of Student)
最后改变这一行...
lstStudents.item.add(newStudent.nameConcat)(newStudent.strFirstName,strLastName)
到...
lstStudents.item.add(Trim(newStudent.strFirstName)&“”& Trim(newStudent.strLastName))
试试看看会发生什么,祝你好运。