我正在尝试使用MySQL在Vb.net中进行聊天,我想将聊天加载到列表框中,其中包含(用户名)''par''和(消息)。
但它一直向我显示错误,我不明白如何解决它。
这是我的代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim stringConn As String
Dim stringCmd As String
Dim myConn As MySqlConnection
Dim myCmd As MySqlCommand
stringCmd = "SELECT par, message FROM chat"
stringConn = "server=localhost; user id=studio; password=mypw; database=studio;"
myConn = New MySqlConnection(stringConn)
myCmd = New MySqlCommand(stringCmd, myConn)
myConn.Open()
Dim myReader As MySqlDataReader
myReader = myCmd.ExecuteReader()
'Reset your List box here.
ListBox1.Items.Clear()
While myReader.Read()
--------------Here is my problem ---------------
ListBox1.Items.Add(myReader.GetString(1//username//) & " " & myReader.GetString(2//message//))
----结束---问题---
End While
myReader.Close()
myConn.Close()
End Sub
答案 0 :(得分:1)
你可以试试这个:
If myReader.HasRows = True Then
Do While myReader.Read()
ListBox1.Items.Add(myReader(0) & " " & myReader(1))
Loop
End If
最好的问候